1   /**
2    * Copyright (c) 2000-2008 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.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.bean.BeanLocatorUtil;
28  import com.liferay.portal.kernel.util.StringMaker;
29  import com.liferay.portal.model.Company;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.theme.ThemeDisplay;
34  
35  import java.io.IOException;
36  
37  import java.rmi.RemoteException;
38  
39  import java.util.Date;
40  import java.util.Locale;
41  import java.util.Map;
42  import java.util.TimeZone;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletMode;
47  import javax.portlet.PortletPreferences;
48  import javax.portlet.PortletRequest;
49  import javax.portlet.PortletResponse;
50  import javax.portlet.PreferencesValidator;
51  import javax.portlet.RenderRequest;
52  import javax.portlet.ValidatorException;
53  import javax.portlet.WindowState;
54  
55  import javax.servlet.ServletContext;
56  import javax.servlet.ServletException;
57  import javax.servlet.http.HttpServletRequest;
58  import javax.servlet.http.HttpServletResponse;
59  import javax.servlet.http.HttpSession;
60  
61  /**
62   * <a href="PortalUtil.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Brian Wing Shun Chan
65   *
66   */
67  public class PortalUtil {
68  
69      public static void clearRequestParameters(RenderRequest req) {
70          getPortal().clearRequestParameters(req);
71      }
72  
73      public static void copyRequestParameters(
74          ActionRequest req, ActionResponse res) {
75  
76          getPortal().copyRequestParameters(req, res);
77      }
78  
79      public static String getCDNHost() {
80          return getPortal().getCDNHost();
81      }
82  
83      public static String getClassName(long classNameId) {
84          return getPortal().getClassName(classNameId);
85      }
86  
87      public static long getClassNameId(Class<?> classObj) {
88          return getPortal().getClassNameId(classObj);
89      }
90  
91      public static long getClassNameId(String value) {
92          return getPortal().getClassNameId(value);
93      }
94  
95      public static String getClassNamePortletId(String className) {
96          return getPortal().getClassNamePortletId(className);
97      }
98  
99      public static Company getCompany(HttpServletRequest req)
100         throws PortalException, SystemException {
101 
102         return getPortal().getCompany(req);
103     }
104 
105     public static Company getCompany(ActionRequest req)
106         throws PortalException, SystemException {
107 
108         return getPortal().getCompany(req);
109     }
110 
111     public static Company getCompany(RenderRequest req)
112         throws PortalException, SystemException {
113 
114         return getPortal().getCompany(req);
115     }
116 
117     public static long getCompanyId(HttpServletRequest req) {
118         return getPortal().getCompanyId(req);
119     }
120 
121     public static long getCompanyId(ActionRequest req) {
122         return getPortal().getCompanyId(req);
123     }
124 
125     public static long getCompanyId(PortletRequest req) {
126         return getPortal().getCompanyId(req);
127     }
128 
129     public static long getCompanyId(RenderRequest req) {
130         return getPortal().getCompanyId(req);
131     }
132 
133     public static long getCompanyIdByWebId(ServletContext ctx) {
134         return getPortal().getCompanyIdByWebId(ctx);
135     }
136 
137     public static long getCompanyIdByWebId(String webId) {
138         return getPortal().getCompanyIdByWebId(webId);
139     }
140 
141     public static String getComputerName() {
142         return getPortal().getComputerName();
143     }
144 
145     public static String getCurrentURL(HttpServletRequest req) {
146         return getPortal().getCurrentURL(req);
147     }
148 
149     public static String getCurrentURL(PortletRequest req) {
150         return getPortal().getCurrentURL(req);
151     }
152 
153     public static Date getDate(int month, int day, int year, PortalException pe)
154         throws PortalException {
155 
156         return getPortal().getDate(month, day, year, pe);
157     }
158 
159     public static Date getDate(
160             int month, int day, int year, TimeZone timeZone, PortalException pe)
161         throws PortalException {
162 
163         return getPortal().getDate(month, day, year, timeZone, pe);
164     }
165 
166     public static Date getDate(
167             int month, int day, int year, int hour, int min, PortalException pe)
168         throws PortalException {
169 
170         return getPortal().getDate(month, day, year, hour, min, pe);
171     }
172 
173     public static Date getDate(
174             int month, int day, int year, int hour, int min, TimeZone timeZone,
175             PortalException pe)
176         throws PortalException {
177 
178         return getPortal().getDate(month, day, year, hour, min, timeZone, pe);
179     }
180 
181     public static String getHost(HttpServletRequest req) {
182         return getPortal().getHost(req);
183     }
184 
185     public static String getHost(ActionRequest req) {
186         return getPortal().getHost(req);
187     }
188 
189     public static String getHost(RenderRequest req) {
190         return getPortal().getHost(req);
191     }
192 
193     public static HttpServletRequest getHttpServletRequest(PortletRequest req) {
194         return getPortal().getHttpServletRequest(req);
195     }
196 
197     public static HttpServletResponse getHttpServletResponse(
198         PortletResponse res) {
199 
200         return getPortal().getHttpServletResponse(res);
201     }
202 
203     public static String getLayoutEditPage(Layout layout) {
204         return getPortal().getLayoutEditPage(layout);
205     }
206 
207     public static String getLayoutViewPage(Layout layout) {
208         return getPortal().getLayoutViewPage(layout);
209     }
210 
211     public static String getLayoutURL(ThemeDisplay themeDisplay)
212         throws PortalException, SystemException {
213 
214         return getPortal().getLayoutURL(themeDisplay);
215     }
216 
217     public static String getLayoutURL(Layout layout, ThemeDisplay themeDisplay)
218         throws PortalException, SystemException {
219 
220         return getPortal().getLayoutURL(layout, themeDisplay);
221     }
222 
223     public static String getLayoutURL(
224             Layout layout, ThemeDisplay themeDisplay, boolean doAsUser)
225         throws PortalException, SystemException {
226 
227         return getPortal().getLayoutURL(layout, themeDisplay, doAsUser);
228     }
229 
230     public static String getLayoutActualURL(Layout layout)
231         throws PortalException, SystemException {
232 
233         return getPortal().getLayoutActualURL(layout);
234     }
235 
236     public static String getLayoutActualURL(Layout layout, String mainPath)
237         throws PortalException, SystemException {
238 
239         return getPortal().getLayoutActualURL(layout, mainPath);
240     }
241 
242     public static String getLayoutActualURL(
243             long groupId, boolean privateLayout, String mainPath,
244             String friendlyURL)
245         throws PortalException, SystemException {
246 
247         return getPortal().getLayoutActualURL(
248             groupId, privateLayout, mainPath, friendlyURL);
249     }
250 
251     public static String getLayoutActualURL(
252             long groupId, boolean privateLayout, String mainPath,
253             String friendlyURL, Map<String, String[]> params)
254         throws PortalException, SystemException {
255 
256         return getPortal().getLayoutActualURL(
257             groupId, privateLayout, mainPath, friendlyURL, params);
258     }
259 
260     public static String getLayoutFriendlyURL(
261             Layout layout, ThemeDisplay themeDisplay)
262         throws PortalException, SystemException {
263 
264         return getPortal().getLayoutFriendlyURL(layout, themeDisplay);
265     }
266 
267     public static String getLayoutTarget(Layout layout) {
268         return getPortal().getLayoutTarget(layout);
269     }
270 
271     public static String getJsSafePortletId(String portletId)  {
272         return getPortal().getJsSafePortletId(portletId);
273     }
274 
275     public static Locale getLocale(HttpServletRequest req) {
276         return getPortal().getLocale(req);
277     }
278 
279     public static Locale getLocale(RenderRequest req) {
280         return getPortal().getLocale(req);
281     }
282 
283     public static HttpServletRequest getOriginalServletRequest(
284         HttpServletRequest req) {
285 
286         return getPortal().getOriginalServletRequest(req);
287     }
288 
289     public static String getPathContext() {
290         return getPortal().getPathContext();
291     }
292 
293     public static String getPathFriendlyURLPrivateGroup() {
294         return getPortal().getPathFriendlyURLPrivateGroup();
295     }
296 
297     public static String getPathFriendlyURLPrivateUser() {
298         return getPortal().getPathFriendlyURLPrivateUser();
299     }
300 
301     public static String getPathFriendlyURLPublic() {
302         return getPortal().getPathFriendlyURLPublic();
303     }
304 
305     public static String getPathImage() {
306         return getPortal().getPathImage();
307     }
308 
309     public static String getPathMain() {
310         return getPortal().getPathMain();
311     }
312 
313     public static long getPlidIdFromFriendlyURL(
314         long companyId, String friendlyURL) {
315 
316         return getPortal().getPlidIdFromFriendlyURL(companyId, friendlyURL);
317     }
318 
319     public static Portal getPortal() {
320         return _getUtil()._portal;
321     }
322 
323     public static String getPortalLibDir() {
324         return getPortal().getPortalLibDir();
325     }
326 
327     public static String getPortalURL(ThemeDisplay themeDisplay) {
328         return getPortal().getPortalURL(themeDisplay);
329     }
330 
331     public static String getPortalURL(HttpServletRequest req) {
332         return getPortal().getPortalURL(req);
333     }
334 
335     public static String getPortalURL(HttpServletRequest req, boolean secure) {
336         return getPortal().getPortalURL(req, secure);
337     }
338 
339     public static String getPortalURL(PortletRequest req) {
340         return getPortal().getPortalURL(req);
341     }
342 
343     public static String getPortalURL(PortletRequest req, boolean secure) {
344         return getPortal().getPortalURL(req, secure);
345     }
346 
347     public static String getPortalURL(
348         String serverName, int serverPort, boolean secure) {
349 
350         return getPortal().getPortalURL(serverName, serverPort, secure);
351     }
352 
353     public static Object[] getPortletFriendlyURLMapper(
354             long groupId, boolean privateLayout, String url)
355         throws PortalException, SystemException {
356 
357         return getPortal().getPortletFriendlyURLMapper(
358             groupId, privateLayout, url);
359     }
360 
361     public static Object[] getPortletFriendlyURLMapper(
362             long groupId, boolean privateLayout, String url,
363             Map<String, String[]> params)
364         throws PortalException, SystemException {
365 
366         return getPortal().getPortletFriendlyURLMapper(
367             groupId, privateLayout, url, params);
368     }
369 
370     public static long getPortletGroupId(long plid) {
371         return getPortal().getPortletGroupId(plid);
372     }
373 
374     public static long getPortletGroupId(Layout layout) {
375         return getPortal().getPortletGroupId(layout);
376     }
377 
378     public static long getPortletGroupId(HttpServletRequest req) {
379         return getPortal().getPortletGroupId(req);
380     }
381 
382     public static long getPortletGroupId(ActionRequest req) {
383         return getPortal().getPortletGroupId(req);
384     }
385 
386     public static long getPortletGroupId(RenderRequest req) {
387         return getPortal().getPortletGroupId(req);
388     }
389 
390     public static String getPortletNamespace(String portletId) {
391         return getPortal().getPortletNamespace(portletId);
392     }
393 
394     public static String getPortletTitle(
395         String portletId, long companyId, String languageId) {
396 
397         return getPortal().getPortletTitle(portletId, companyId, languageId);
398     }
399 
400     public static String getPortletTitle(
401         String portletId, long companyId, Locale locale) {
402 
403         return getPortal().getPortletTitle(portletId, companyId, locale);
404     }
405 
406     public static String getPortletTitle(String portletId, User user) {
407         return getPortal().getPortletTitle(portletId, user);
408     }
409 
410     public static String getPortletTitle(
411         Portlet portlet, ServletContext ctx, Locale locale) {
412 
413         return getPortal().getPortletTitle(portlet, ctx, locale);
414     }
415 
416     public static String getPortletXmlFileName()
417         throws PortalException, SystemException {
418 
419         return getPortal().getPortletXmlFileName();
420     }
421 
422     public static PortletPreferences getPreferences(HttpServletRequest req) {
423         return getPortal().getPreferences(req);
424     }
425 
426     public static PreferencesValidator getPreferencesValidator(
427         Portlet portlet) {
428 
429         return getPortal().getPreferencesValidator(portlet);
430     }
431 
432     public static User getSelectedUser(HttpServletRequest req)
433         throws PortalException, RemoteException, SystemException {
434 
435         return getPortal().getSelectedUser(req);
436     }
437 
438     public static User getSelectedUser(
439             HttpServletRequest req, boolean checkPermission)
440         throws PortalException, RemoteException, SystemException {
441 
442         return getPortal().getSelectedUser(req, checkPermission);
443     }
444 
445     public static User getSelectedUser(ActionRequest req)
446         throws PortalException, RemoteException, SystemException {
447 
448         return getPortal().getSelectedUser(req);
449     }
450 
451     public static User getSelectedUser(
452             ActionRequest req, boolean checkPermission)
453         throws PortalException, RemoteException, SystemException {
454 
455         return getPortal().getSelectedUser(req, checkPermission);
456     }
457 
458     public static User getSelectedUser(RenderRequest req)
459         throws PortalException, RemoteException, SystemException {
460 
461         return getPortal().getSelectedUser(req);
462     }
463 
464     public static User getSelectedUser(
465             RenderRequest req, boolean checkPermission)
466         throws PortalException, RemoteException, SystemException {
467 
468         return getPortal().getSelectedUser(req, checkPermission);
469     }
470 
471     public static String getStrutsAction(HttpServletRequest req) {
472         return getPortal().getStrutsAction(req);
473     }
474 
475     public static String[] getSystemCommunityRoles() {
476         return getPortal().getSystemCommunityRoles();
477     }
478 
479     public static String[] getSystemGroups() {
480         return getPortal().getSystemGroups();
481     }
482 
483     public static String[] getSystemOrganizationRoles() {
484         return getPortal().getSystemOrganizationRoles();
485     }
486 
487     public static String[] getSystemRoles() {
488         return getPortal().getSystemRoles();
489     }
490 
491     public static Date getUptime() {
492         return getPortal().getUptime();
493     }
494 
495     public static String getURLWithSessionId(String url, String sessionId) {
496         return getPortal().getURLWithSessionId(url, sessionId);
497     }
498 
499     public static User getUser(HttpServletRequest req)
500         throws PortalException, SystemException {
501 
502         return getPortal().getUser(req);
503     }
504 
505     public static User getUser(ActionRequest req)
506         throws PortalException, SystemException {
507 
508         return getPortal().getUser(req);
509     }
510 
511     public static User getUser(RenderRequest req)
512         throws PortalException, SystemException {
513 
514         return getPortal().getUser(req);
515     }
516 
517     public static long getUserId(HttpServletRequest req) {
518         return getPortal().getUserId(req);
519     }
520 
521     public static long getUserId(ActionRequest req) {
522         return getPortal().getUserId(req);
523     }
524 
525     public static long getUserId(RenderRequest req) {
526         return getPortal().getUserId(req);
527     }
528 
529     public static String getUserName(long userId, String defaultUserName) {
530         return getPortal().getUserName(userId, defaultUserName);
531     }
532 
533     public static String getUserName(
534         long userId, String defaultUserName, String userAttribute) {
535 
536         return getPortal().getUserName(userId, defaultUserName, userAttribute);
537     }
538 
539     public static String getUserName(
540         long userId, String defaultUserName, HttpServletRequest req) {
541 
542         return getPortal().getUserName(userId, defaultUserName, req);
543     }
544 
545     public static String getUserName(
546         long userId, String defaultUserName, String userAttribute,
547         HttpServletRequest req) {
548 
549         return getPortal().getUserName(
550             userId, defaultUserName, userAttribute, req);
551     }
552 
553     public static String getUserPassword(HttpSession ses) {
554         return getPortal().getUserPassword(ses);
555     }
556 
557     public static String getUserPassword(HttpServletRequest req) {
558         return getPortal().getUserPassword(req);
559     }
560 
561     public static String getUserPassword(ActionRequest req) {
562         return getPortal().getUserPassword(req);
563     }
564 
565     public static String getUserPassword(RenderRequest req) {
566         return getPortal().getUserPassword(req);
567     }
568 
569     public static String getUserValue(
570             long userId, String param, String defaultValue)
571         throws SystemException {
572 
573         return getPortal().getUserValue(userId, param, defaultValue);
574     }
575 
576     public static boolean isMethodGet(PortletRequest req) {
577         return getPortal().isMethodGet(req);
578     }
579 
580     public static boolean isMethodPost(PortletRequest req) {
581         return getPortal().isMethodPost(req);
582     }
583 
584     public static boolean isLayoutFriendliable(Layout layout) {
585         return getPortal().isLayoutFriendliable(layout);
586     }
587 
588     public static boolean isLayoutParentable(Layout layout) {
589         return getPortal().isLayoutParentable(layout);
590     }
591 
592     public static boolean isLayoutParentable(String type) {
593         return getPortal().isLayoutParentable(type);
594     }
595 
596     public static boolean isLayoutSitemapable(Layout layout) {
597         return getPortal().isLayoutSitemapable(layout);
598     }
599 
600     public static boolean isReservedParameter(String name) {
601         return getPortal().isReservedParameter(name);
602     }
603 
604     public static boolean isSystemGroup(String groupName) {
605         return getPortal().isSystemGroup(groupName);
606     }
607 
608     public static boolean isSystemRole(String roleName) {
609         return getPortal().isSystemRole(roleName);
610     }
611 
612     public static boolean isUpdateAvailable()
613         throws PortalException, SystemException {
614 
615         return getPortal().isUpdateAvailable();
616     }
617 
618     public static void renderPage(
619             StringMaker sm, ServletContext ctx, HttpServletRequest req,
620             HttpServletResponse res, String path)
621         throws IOException, ServletException {
622 
623         getPortal().renderPage(sm, ctx, req, res, path);
624     }
625 
626     public static void renderPortlet(
627             StringMaker sm, ServletContext ctx, HttpServletRequest req,
628             HttpServletResponse res, Portlet portlet, String queryString)
629         throws IOException, ServletException {
630 
631         getPortal().renderPortlet(
632             sm, ctx, req, res, portlet, queryString);
633     }
634 
635     public static void renderPortlet(
636             StringMaker sm, ServletContext ctx, HttpServletRequest req,
637             HttpServletResponse res, Portlet portlet, String queryString,
638             String columnId, Integer columnPos, Integer columnCount)
639         throws IOException, ServletException {
640 
641         getPortal().renderPortlet(
642             sm, ctx, req, res, portlet, queryString, columnId, columnPos,
643             columnCount);
644     }
645 
646     public static void renderPortlet(
647             StringMaker sm, ServletContext ctx, HttpServletRequest req,
648             HttpServletResponse res, Portlet portlet, String queryString,
649             String columnId, Integer columnPos, Integer columnCount,
650             String path)
651         throws IOException, ServletException {
652 
653         getPortal().renderPortlet(
654             sm, ctx, req, res, portlet, queryString, columnId, columnPos,
655             columnCount, path);
656     }
657 
658     public static void sendError(
659             int status, Exception e, HttpServletRequest req,
660             HttpServletResponse res)
661         throws IOException, ServletException {
662 
663         getPortal().sendError(status, e, req, res);
664     }
665 
666     /**
667      * Sets the subtitle for a page. This is just a hint and can be overridden
668      * by subsequent calls. The last call to this method wins.
669      *
670      * @param       subtitle the subtitle for a page
671      * @param       req the HTTP servlet request
672      */
673     public static void setPageSubtitle(
674         String subtitle, HttpServletRequest req) {
675 
676         getPortal().setPageSubtitle(subtitle, req);
677     }
678 
679     /**
680      * Sets the whole title for a page. This is just a hint and can be
681      * overridden by subsequent calls. The last call to this method wins.
682      *
683      * @param       title the whole title for a page
684      * @param       req the HTTP servlet request
685      */
686     public static void setPageTitle(String title, HttpServletRequest req) {
687         getPortal().setPageTitle(title, req);
688     }
689 
690     public static void storePreferences(PortletPreferences prefs)
691         throws IOException, ValidatorException {
692 
693         getPortal().storePreferences(prefs);
694     }
695 
696     public static PortletMode updatePortletMode(
697             String portletId, User user, Layout layout, PortletMode portletMode,
698             HttpServletRequest req)
699         throws PortalException, RemoteException, SystemException {
700 
701         return getPortal().updatePortletMode(
702             portletId, user, layout, portletMode, req);
703     }
704 
705     public static WindowState updateWindowState(
706             String portletId, User user, Layout layout, WindowState windowState,
707             HttpServletRequest req)
708         throws PortalException, RemoteException, SystemException {
709 
710         return getPortal().updateWindowState(
711             portletId, user, layout, windowState, req);
712     }
713 
714     public void setPortal(Portal portal) {
715         _portal = portal;
716     }
717 
718     private static PortalUtil _getUtil() {
719         if (_util == null) {
720             _util = (PortalUtil)BeanLocatorUtil.locate(_UTIL);
721         }
722 
723         return _util;
724     }
725 
726     private static final String _UTIL = PortalUtil.class.getName();
727 
728     private static PortalUtil _util;
729 
730     private Portal _portal;
731 
732 }