1
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.language.LanguageUtil;
32 import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
33 import com.liferay.portal.kernel.portlet.LiferayPortletMode;
34 import com.liferay.portal.kernel.portlet.LiferayWindowState;
35 import com.liferay.portal.kernel.portlet.PortletRequestWrapper;
36 import com.liferay.portal.kernel.portlet.PortletResponseWrapper;
37 import com.liferay.portal.kernel.security.permission.ActionKeys;
38 import com.liferay.portal.kernel.servlet.StringServletResponse;
39 import com.liferay.portal.kernel.util.ArrayUtil;
40 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
41 import com.liferay.portal.kernel.util.ContentTypes;
42 import com.liferay.portal.kernel.util.GetterUtil;
43 import com.liferay.portal.kernel.util.InstancePool;
44 import com.liferay.portal.kernel.util.JavaConstants;
45 import com.liferay.portal.kernel.util.LocaleUtil;
46 import com.liferay.portal.kernel.util.ParamUtil;
47 import com.liferay.portal.kernel.util.StringComparator;
48 import com.liferay.portal.kernel.util.StringMaker;
49 import com.liferay.portal.kernel.util.StringPool;
50 import com.liferay.portal.kernel.util.StringUtil;
51 import com.liferay.portal.kernel.util.Validator;
52 import com.liferay.portal.model.ClassName;
53 import com.liferay.portal.model.Company;
54 import com.liferay.portal.model.Group;
55 import com.liferay.portal.model.Layout;
56 import com.liferay.portal.model.LayoutSet;
57 import com.liferay.portal.model.LayoutTypePortlet;
58 import com.liferay.portal.model.Portlet;
59 import com.liferay.portal.model.User;
60 import com.liferay.portal.model.impl.GroupImpl;
61 import com.liferay.portal.model.impl.LayoutImpl;
62 import com.liferay.portal.model.impl.RoleImpl;
63 import com.liferay.portal.plugin.PluginPackageUtil;
64 import com.liferay.portal.security.permission.PermissionCheckerFactory;
65 import com.liferay.portal.security.permission.PermissionCheckerImpl;
66 import com.liferay.portal.service.ClassNameServiceUtil;
67 import com.liferay.portal.service.CompanyLocalServiceUtil;
68 import com.liferay.portal.service.LayoutLocalServiceUtil;
69 import com.liferay.portal.service.PortletLocalServiceUtil;
70 import com.liferay.portal.service.UserLocalServiceUtil;
71 import com.liferay.portal.service.UserServiceUtil;
72 import com.liferay.portal.service.permission.UserPermissionUtil;
73 import com.liferay.portal.servlet.PortletContextPool;
74 import com.liferay.portal.servlet.PortletContextWrapper;
75 import com.liferay.portal.theme.ThemeDisplay;
76 import com.liferay.portlet.ActionRequestImpl;
77 import com.liferay.portlet.ActionResponseImpl;
78 import com.liferay.portlet.CachePortlet;
79 import com.liferay.portlet.PortletConfigFactory;
80 import com.liferay.portlet.PortletPreferencesImpl;
81 import com.liferay.portlet.PortletPreferencesWrapper;
82 import com.liferay.portlet.PortletURLImpl;
83 import com.liferay.portlet.RenderRequestImpl;
84 import com.liferay.portlet.RenderResponseImpl;
85 import com.liferay.portlet.UserAttributes;
86 import com.liferay.portlet.wsrp.URLGeneratorImpl;
87 import com.liferay.util.CollectionFactory;
88 import com.liferay.util.Encryptor;
89 import com.liferay.util.Http;
90 import com.liferay.util.HttpUtil;
91 import com.liferay.util.JS;
92 import com.liferay.util.servlet.DynamicServletRequest;
93 import com.liferay.util.servlet.UploadPortletRequest;
94 import com.liferay.util.servlet.UploadServletRequest;
95 import com.liferay.util.xml.XMLSafeReader;
96
97 import java.io.File;
98 import java.io.IOException;
99 import java.io.InputStream;
100
101 import java.net.URL;
102
103 import java.rmi.RemoteException;
104
105 import java.security.Key;
106
107 import java.util.Arrays;
108 import java.util.Calendar;
109 import java.util.Date;
110 import java.util.Enumeration;
111 import java.util.HashMap;
112 import java.util.Iterator;
113 import java.util.List;
114 import java.util.Locale;
115 import java.util.Map;
116 import java.util.Properties;
117 import java.util.ResourceBundle;
118 import java.util.Set;
119 import java.util.TimeZone;
120
121 import javax.portlet.ActionRequest;
122 import javax.portlet.ActionResponse;
123 import javax.portlet.PortletConfig;
124 import javax.portlet.PortletMode;
125 import javax.portlet.PortletPreferences;
126 import javax.portlet.PortletRequest;
127 import javax.portlet.PortletResponse;
128 import javax.portlet.PortletURL;
129 import javax.portlet.PreferencesValidator;
130 import javax.portlet.RenderRequest;
131 import javax.portlet.ValidatorException;
132 import javax.portlet.WindowState;
133
134 import javax.servlet.RequestDispatcher;
135 import javax.servlet.ServletContext;
136 import javax.servlet.ServletException;
137 import javax.servlet.ServletRequest;
138 import javax.servlet.http.HttpServletRequest;
139 import javax.servlet.http.HttpServletRequestWrapper;
140 import javax.servlet.http.HttpServletResponse;
141 import javax.servlet.http.HttpSession;
142
143 import org.apache.commons.logging.Log;
144 import org.apache.commons.logging.LogFactory;
145 import org.apache.struts.Globals;
146
147 import org.dom4j.Document;
148 import org.dom4j.DocumentException;
149 import org.dom4j.io.SAXReader;
150
151 import org.hibernate.util.FastHashMap;
152
153
161 public class PortalUtil {
162
163 public static final boolean DEFAULT_P_L_RESET = GetterUtil.getBoolean(
164 PropsUtil.get(PropsUtil.LAYOUT_DEFAULT_P_L_RESET));
165
166 public static final String PATH_IMAGE = "/image";
167
168 public static final String PATH_MAIN = "/c";
169
170 public static final String PATH_PORTAL_LAYOUT = "/portal/layout";
171
172 public static final String PORTLET_XML_FILE_NAME_STANDARD = "portlet.xml";
173
174 public static final String PORTLET_XML_FILE_NAME_CUSTOM =
175 "portlet-custom.xml";
176
177 public static final Date UP_TIME = new Date();
178
179 public static void clearRequestParameters(RenderRequest req) {
180
181
183 boolean action = ParamUtil.getBoolean(req, "p_p_action");
184
185 if (action) {
186 ((RenderRequestImpl)req).getRenderParameters().clear();
187 }
188 }
189
190 public static void copyRequestParameters(
191 ActionRequest req, ActionResponse res) {
192
193 try {
194 ActionResponseImpl resImpl = (ActionResponseImpl)res;
195
196 Map renderParameters = resImpl.getRenderParameters();
197
198 res.setRenderParameter("p_p_action", "1");
199
200 Enumeration enu = req.getParameterNames();
201
202 while (enu.hasMoreElements()) {
203 String param = (String)enu.nextElement();
204 String[] values = req.getParameterValues(param);
205
206 if (renderParameters.get(
207 resImpl.getNamespace() + param) == null) {
208
209 res.setRenderParameter(param, values);
210 }
211 }
212 }
213 catch (IllegalStateException ise) {
214
215
218 }
219 }
220
221 public static String createSecureProxyURL(
222 String url, Key key, boolean secure) {
223
224
227 if (!secure) {
228 return url;
229 }
230 else {
231 Map params = CollectionFactory.getHashMap();
232
233 params.put(org.apache.wsrp4j.util.Constants.URL, url);
234
235 return URLGeneratorImpl.getResourceProxyURL(params, key);
236 }
237 }
238
239 public static String getCDNHost() {
240 return _instance._getCDNHost();
241 }
242
243 public static String getClassName(long classNameId) {
244 try {
245 ClassName className = ClassNameServiceUtil.getClassName(
246 classNameId);
247
248 return className.getValue();
249 }
250 catch (Exception e) {
251 throw new RuntimeException(
252 "Unable to get class name from id " + classNameId);
253 }
254 }
255
256 public static long getClassNameId(Class classObj) {
257 return getClassNameId(classObj.getName());
258 }
259
260 public static long getClassNameId(String value) {
261 try {
262 ClassName className = ClassNameServiceUtil.getClassName(value);
263
264 return className.getClassNameId();
265 }
266 catch (Exception e) {
267 throw new RuntimeException(
268 "Unable to get class name from value " + value);
269 }
270 }
271
272 public static String getClassNamePortletId(String className) {
273 String portletId = StringPool.BLANK;
274
275 if (className.startsWith("com.liferay.portlet.blogs")) {
276 portletId = PortletKeys.BLOGS;
277 }
278 else if (className.startsWith("com.liferay.portlet.bookmarks")) {
279 portletId = PortletKeys.BOOKMARKS;
280 }
281 else if (className.startsWith("com.liferay.portlet.documentlibrary")) {
282 portletId = PortletKeys.DOCUMENT_LIBRARY;
283 }
284 else if (className.startsWith("com.liferay.portlet.imagegallery")) {
285 portletId = PortletKeys.IMAGE_GALLERY;
286 }
287 else if (className.startsWith("com.liferay.portlet.journal")) {
288 portletId = PortletKeys.JOURNAL;
289 }
290 else if (className.startsWith("com.liferay.portlet.messageboards")) {
291 portletId = PortletKeys.MESSAGE_BOARDS;
292 }
293 else if (className.startsWith("com.liferay.portlet.wiki")) {
294 portletId = PortletKeys.WIKI;
295 }
296
297 return portletId;
298 }
299
300 public static Company getCompany(HttpServletRequest req)
301 throws PortalException, SystemException {
302
303 long companyId = getCompanyId(req);
304
305 if (companyId <= 0) {
306 return null;
307 }
308
309 Company company = (Company)req.getAttribute(WebKeys.COMPANY);
310
311 if (company == null) {
312 company = CompanyLocalServiceUtil.getCompanyById(companyId);
313
314 req.setAttribute(WebKeys.COMPANY, company);
315 }
316
317 return company;
318 }
319
320 public static Company getCompany(ActionRequest req)
321 throws PortalException, SystemException {
322
323 return getCompany(getHttpServletRequest(req));
324 }
325
326 public static Company getCompany(RenderRequest req)
327 throws PortalException, SystemException {
328
329 return getCompany(getHttpServletRequest(req));
330 }
331
332 public static long getCompanyId(HttpServletRequest req) {
333 Long companyIdObj = (Long)req.getAttribute(WebKeys.COMPANY_ID);
334
335 if (companyIdObj != null) {
336 return companyIdObj.longValue();
337 }
338 else {
339 return 0;
340 }
341 }
342
343 public static long getCompanyId(ActionRequest req) {
344 return getCompanyId(getHttpServletRequest(req));
345 }
346
347 public static long getCompanyId(PortletRequest req) {
348 long companyId = 0;
349
350 if (req instanceof ActionRequest) {
351 companyId = getCompanyId((ActionRequest)req);
352 }
353 else {
354 companyId = getCompanyId((RenderRequest)req);
355 }
356
357 return companyId;
358 }
359
360 public static long getCompanyId(RenderRequest req) {
361 return getCompanyId(getHttpServletRequest(req));
362 }
363
364 public static long getCompanyIdByWebId(ServletContext ctx) {
365 String webId = GetterUtil.getString(
366 ctx.getInitParameter("company_web_id"));
367
368 return getCompanyIdByWebId(webId);
369 }
370
371 public static long getCompanyIdByWebId(String webId) {
372 long companyId = 0;
373
374 try {
375 Company company = CompanyLocalServiceUtil.getCompanyByWebId(webId);
376
377 companyId = company.getCompanyId();
378 }
379 catch (Exception e) {
380 _log.error(e.getMessage());
381 }
382
383 return companyId;
384 }
385
386 public static String getCurrentURL(HttpServletRequest req) {
387 String currentURL = (String)req.getAttribute(WebKeys.CURRENT_URL);
388
389 if (currentURL == null) {
390 currentURL = ParamUtil.getString(req, "currentURL");
391
392 if (Validator.isNull(currentURL)) {
393 if (true) {
394 currentURL = Http.getCompleteURL(req);
395 }
396 else {
397
398
400 currentURL = _getCurrentURL(req);
401 }
402
403 if ((Validator.isNotNull(currentURL)) &&
404 (currentURL.indexOf("j_security_check") == -1)) {
405
406 currentURL = currentURL.substring(
407 currentURL.indexOf("://") + 3, currentURL.length());
408
409 currentURL = currentURL.substring(
410 currentURL.indexOf("/"), currentURL.length());
411 }
412 }
413
414 if (Validator.isNull(currentURL)) {
415 currentURL = getPathMain();
416 }
417
418 req.setAttribute(WebKeys.CURRENT_URL, currentURL);
419 }
420
421 return currentURL;
422 }
423
424 public static Date getDate(
425 int month, int day, int year, PortalException pe)
426 throws PortalException {
427
428 return getDate(month, day, year, null, pe);
429 }
430
431 public static Date getDate(
432 int month, int day, int year, TimeZone timeZone, PortalException pe)
433 throws PortalException {
434
435 return getDate(month, day, year, -1, -1, timeZone, pe);
436 }
437
438 public static Date getDate(
439 int month, int day, int year, int hour, int min, PortalException pe)
440 throws PortalException {
441
442 return getDate(month, day, year, hour, min, null, pe);
443 }
444
445 public static Date getDate(
446 int month, int day, int year, int hour, int min, TimeZone timeZone,
447 PortalException pe)
448 throws PortalException {
449
450 if (!Validator.isGregorianDate(month, day, year)) {
451 throw pe;
452 }
453 else {
454 Calendar cal = null;
455
456 if (timeZone == null) {
457 cal = CalendarFactoryUtil.getCalendar();
458 }
459 else {
460 cal = CalendarFactoryUtil.getCalendar(timeZone);
461 }
462
463 if ((hour == -1) || (min == -1)) {
464 cal.set(year, month, day);
465 }
466 else {
467 cal.set(year, month, day, hour, min, 0);
468 }
469
470 Date date = cal.getTime();
471
472
477
478 return date;
479 }
480 }
481
482 public static String getHost(HttpServletRequest req) {
483 String host = req.getHeader("Host");
484
485 if (host != null) {
486 host = host.trim().toLowerCase();
487
488 int pos = host.indexOf(':');
489
490 if (pos >= 0) {
491 host = host.substring(0, pos);
492 }
493 }
494 else {
495 host = null;
496 }
497
498 return host;
499 }
500
501 public static String getHost(ActionRequest req) {
502 return getHost(getHttpServletRequest(req));
503 }
504
505 public static String getHost(RenderRequest req) {
506 return getHost(getHttpServletRequest(req));
507 }
508
509 public static HttpServletRequest getHttpServletRequest(PortletRequest req) {
510 if (req instanceof ActionRequestImpl) {
511 ActionRequestImpl reqImpl = (ActionRequestImpl)req;
512
513 return reqImpl.getHttpServletRequest();
514 }
515 else if (req instanceof RenderRequestImpl) {
516 RenderRequestImpl reqImpl = (RenderRequestImpl)req;
517
518 return reqImpl.getHttpServletRequest();
519 }
520 else if (req instanceof PortletRequestWrapper) {
521 PortletRequestWrapper reqWrapper = (PortletRequestWrapper)req;
522
523 return getHttpServletRequest(reqWrapper.getPortletRequest());
524 }
525 else {
526 throw new RuntimeException(
527 "Unable to get the HTTP servlet request from " +
528 req.getClass().getName());
529 }
530 }
531
532 public static HttpServletResponse getHttpServletResponse(
533 PortletResponse res) {
534
535 if (res instanceof ActionResponseImpl) {
536 ActionResponseImpl resImpl = (ActionResponseImpl)res;
537
538 return resImpl.getHttpServletResponse();
539 }
540 else if (res instanceof RenderResponseImpl) {
541 RenderResponseImpl resImpl = (RenderResponseImpl)res;
542
543 return resImpl.getHttpServletResponse();
544 }
545 else if (res instanceof PortletResponseWrapper) {
546 PortletResponseWrapper resWrapper = (PortletResponseWrapper)res;
547
548 return getHttpServletResponse(resWrapper.getPortletResponse());
549 }
550 else {
551 throw new RuntimeException(
552 "Unable to get the HTTP servlet resuest from " +
553 res.getClass().getName());
554 }
555 }
556
557 public static String getLayoutEditPage(Layout layout) {
558 return PropsUtil.getComponentProperties().getString(
559 PropsUtil.LAYOUT_EDIT_PAGE, Filter.by(layout.getType()));
560 }
561
562 public static String getLayoutViewPage(Layout layout) {
563 return PropsUtil.getComponentProperties().getString(
564 PropsUtil.LAYOUT_VIEW_PAGE, Filter.by(layout.getType()));
565 }
566
567 public static String getLayoutURL(ThemeDisplay themeDisplay)
568 throws PortalException, SystemException {
569
570 return getLayoutURL(themeDisplay.getLayout(), themeDisplay);
571 }
572
573 public static String getLayoutURL(Layout layout, ThemeDisplay themeDisplay)
574 throws PortalException, SystemException {
575
576 return getLayoutURL(layout, themeDisplay, true);
577 }
578
579 public static String getLayoutURL(
580 Layout layout, ThemeDisplay themeDisplay, boolean doAsUser)
581 throws PortalException, SystemException {
582
583 if (layout == null) {
584 return themeDisplay.getPathMain() + PATH_PORTAL_LAYOUT;
585 }
586
587 if (!layout.getType().equals(LayoutImpl.TYPE_URL)) {
588 String layoutFriendlyURL = getLayoutFriendlyURL(
589 layout, themeDisplay);
590
591 if (Validator.isNotNull(layoutFriendlyURL)) {
592 if (doAsUser &&
593 Validator.isNotNull(themeDisplay.getDoAsUserId())) {
594
595 layoutFriendlyURL = Http.addParameter(
596 layoutFriendlyURL, "doAsUserId",
597 themeDisplay.getDoAsUserId());
598 }
599
600 return layoutFriendlyURL;
601 }
602 }
603
604 String layoutURL = getLayoutActualURL(layout);
605
606 if (doAsUser && Validator.isNotNull(themeDisplay.getDoAsUserId())) {
607 layoutURL = Http.addParameter(
608 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
609 }
610
611 return layoutURL;
612 }
613
614 public static String getLayoutActualURL(Layout layout)
615 throws PortalException, SystemException {
616
617 return getLayoutActualURL(layout, getPathMain());
618 }
619
620 public static String getLayoutActualURL(Layout layout, String mainPath)
621 throws PortalException, SystemException {
622
623 Map vars = new FastHashMap();
624
625 vars.put("liferay:mainPath", mainPath);
626 vars.put("liferay:plid", String.valueOf(layout.getPlid()));
627 vars.putAll(layout.getLayoutType().getTypeSettingsProperties());
628
629 String href = PropsUtil.getComponentProperties().getString(
630 PropsUtil.LAYOUT_URL,
631 Filter.by(layout.getType()).setVariables(vars));
632
633 return href;
634 }
635
636 public static String getLayoutActualURL(
637 long groupId, boolean privateLayout, String mainPath,
638 String friendlyURL)
639 throws PortalException, SystemException {
640
641 return getLayoutActualURL(
642 groupId, privateLayout, mainPath, friendlyURL, null);
643 }
644
645 public static String getLayoutActualURL(
646 long groupId, boolean privateLayout, String mainPath,
647 String friendlyURL, Map params)
648 throws PortalException, SystemException {
649
650 Layout layout = null;
651 String queryString = StringPool.BLANK;
652
653 if (Validator.isNull(friendlyURL)) {
654 List layouts = LayoutLocalServiceUtil.getLayouts(
655 groupId, privateLayout, LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
656
657 if (layouts.size() > 0) {
658 layout = (Layout)layouts.get(0);
659 }
660 else {
661 throw new NoSuchLayoutException(
662 "{groupId=" + groupId + ",privateLayout=" + privateLayout +
663 "} does not have any layouts");
664 }
665 }
666 else {
667 Object[] friendlyURLMapper = getPortletFriendlyURLMapper(
668 groupId, privateLayout, friendlyURL, params);
669
670 layout = (Layout)friendlyURLMapper[0];
671 queryString = (String)friendlyURLMapper[1];
672 }
673
674 String layoutActualURL = getLayoutActualURL(layout, mainPath);
675
676 if (Validator.isNotNull(queryString)) {
677 layoutActualURL = layoutActualURL + queryString;
678 }
679
680 return layoutActualURL;
681 }
682
683 public static String getLayoutFriendlyURL(
684 Layout layout, ThemeDisplay themeDisplay)
685 throws PortalException, SystemException {
686
687 if (!isLayoutFriendliable(layout)) {
688 return null;
689 }
690
691 String layoutFriendlyURL = layout.getFriendlyURL();
692
693 if (Validator.isNull(layoutFriendlyURL)) {
694 layoutFriendlyURL = layout.getDefaultFriendlyURL();
695 }
696
697 LayoutSet layoutSet = layout.getLayoutSet();
698
699 if (Validator.isNotNull(layoutSet.getVirtualHost())) {
700 String portalURL = getPortalURL(
701 layoutSet.getVirtualHost(), themeDisplay.getServerPort(),
702 themeDisplay.isSecure());
703
704
707 long curLayoutSetId =
708 themeDisplay.getLayout().getLayoutSet().getLayoutSetId();
709
710 if ((layoutSet.getLayoutSetId() != curLayoutSetId) ||
711 (portalURL.startsWith(themeDisplay.getURLPortal()))) {
712
713 return portalURL + getPathContext() + layoutFriendlyURL;
714 }
715 }
716
717 Group group = layout.getGroup();
718
719 String parentFriendlyURL = group.getFriendlyURL();
720
721 if (Validator.isNull(parentFriendlyURL)) {
722 parentFriendlyURL = group.getDefaultFriendlyURL(
723 layout.isPrivateLayout());
724 }
725
726 String friendlyURL = null;
727
728 if (layout.isPrivateLayout()) {
729 if (group.isUser()) {
730 friendlyURL = getPathFriendlyURLPrivateUser();
731 }
732 else {
733 friendlyURL = getPathFriendlyURLPrivateGroup();
734 }
735 }
736 else {
737 friendlyURL = getPathFriendlyURLPublic();
738 }
739
740 return friendlyURL + parentFriendlyURL + layoutFriendlyURL;
741 }
742
743 public static String getLayoutTarget(Layout layout) {
744 Properties typeSettingsProps = layout.getTypeSettingsProperties();
745
746 String target = typeSettingsProps.getProperty("target");
747
748 if (Validator.isNull(target)) {
749 target = StringPool.BLANK;
750 }
751 else {
752 target = "target=\"" + target + "\"";
753 }
754
755 return target;
756 }
757
758 public static String getJsSafePortletId(String portletId) {
759 return JS.getSafeName(portletId);
760 }
761
762
765 public static String getJsSafePortletName(String portletName) {
766 return JS.getSafeName(portletName);
767 }
768
769 public static Locale getLocale(HttpServletRequest req) {
770 return (Locale)req.getSession().getAttribute(Globals.LOCALE_KEY);
771 }
772
773 public static Locale getLocale(RenderRequest req) {
774 return getLocale(getHttpServletRequest(req));
775 }
776
777 public static HttpServletRequest getOriginalServletRequest(
778 HttpServletRequest req) {
779
780 HttpServletRequest originalReq = req;
781
782 while (originalReq.getClass().getName().startsWith("com.liferay.")) {
783
784
787 originalReq = (HttpServletRequest)
788 ((HttpServletRequestWrapper)originalReq).getRequest();
789 }
790
791 return originalReq;
792 }
793
794 public static String getPathContext() {
795 return _instance._getPathContext();
796 }
797
798 public static String getPathFriendlyURLPrivateGroup() {
799 return _instance._getPathFriendlyURLPrivateGroup();
800 }
801
802 public static String getPathFriendlyURLPrivateUser() {
803 return _instance._getPathFriendlyURLPrivateUser();
804 }
805
806 public static String getPathFriendlyURLPublic() {
807 return _instance._getPathFriendlyURLPublic();
808 }
809
810 public static String getPathImage() {
811 return _instance._getPathImage();
812 }
813
814 public static String getPathMain() {
815 return _instance._getPathMain();
816 }
817
818 public static String getPortalLibDir() {
819 return _instance._portalLibDir;
820 }
821
822 public static String getPortalURL(ThemeDisplay themeDisplay) {
823 String serverName = themeDisplay.getServerName();
824
825 Layout layout = themeDisplay.getLayout();
826
827 if (layout != null) {
828 LayoutSet layoutSet = layout.getLayoutSet();
829
830 if (Validator.isNotNull(layoutSet.getVirtualHost())) {
831 serverName = layoutSet.getVirtualHost();
832 }
833 }
834
835 return getPortalURL(
836 serverName, themeDisplay.getServerPort(), themeDisplay.isSecure());
837 }
838
839 public static String getPortalURL(HttpServletRequest req) {
840 return getPortalURL(req, req.isSecure());
841 }
842
843 public static String getPortalURL(HttpServletRequest req, boolean secure) {
844 return getPortalURL(req.getServerName(), req.getServerPort(), secure);
845 }
846
847 public static String getPortalURL(PortletRequest req) {
848 return getPortalURL(req, req.isSecure());
849 }
850
851 public static String getPortalURL(PortletRequest req, boolean secure) {
852 return getPortalURL(req.getServerName(), req.getServerPort(), secure);
853 }
854
855 public static String getPortalURL(
856 String serverName, int serverPort, boolean secure) {
857
858 StringMaker sm = new StringMaker();
859
860 String serverProtocol = GetterUtil.getString(
861 PropsUtil.get(PropsUtil.WEB_SERVER_PROTOCOL));
862
863 if (secure || Http.HTTPS.equals(serverProtocol)) {
864 sm.append(Http.HTTPS_WITH_SLASH);
865 }
866 else {
867 sm.append(Http.HTTP_WITH_SLASH);
868 }
869
870 String serverHost = PropsUtil.get(PropsUtil.WEB_SERVER_HOST);
871
872 if (Validator.isNull(serverHost)) {
873 sm.append(serverName);
874 }
875 else {
876 sm.append(serverHost);
877 }
878
879 int serverHttpPort = GetterUtil.getInteger(
880 PropsUtil.get(PropsUtil.WEB_SERVER_HTTP_PORT), -1);
881
882 if (!secure) {
883 if (serverHttpPort == -1) {
884 if ((serverPort != Http.HTTP_PORT) &&
885 (serverPort != Http.HTTPS_PORT)) {
886
887 sm.append(StringPool.COLON);
888 sm.append(serverPort);
889 }
890 }
891 else {
892 if ((serverPort != serverHttpPort) ||
893 (serverHttpPort != Http.HTTP_PORT)) {
894
895 sm.append(StringPool.COLON);
896 sm.append(serverHttpPort);
897 }
898 }
899 }
900
901 int serverHttpsPort = GetterUtil.getInteger(
902 PropsUtil.get(PropsUtil.WEB_SERVER_HTTPS_PORT), -1);
903
904 if (secure) {
905 if (serverHttpsPort == -1) {
906 if ((serverPort != Http.HTTP_PORT) &&
907 (serverPort != Http.HTTPS_PORT)) {
908
909 sm.append(StringPool.COLON);
910 sm.append(serverPort);
911 }
912 }
913 else {
914 if ((serverPort != serverHttpsPort) ||
915 (serverHttpsPort != Http.HTTPS_PORT)) {
916
917 sm.append(StringPool.COLON);
918 sm.append(serverHttpsPort);
919 }
920 }
921 }
922
923 return sm.toString();
924 }
925
926 public static Object[] getPortletFriendlyURLMapper(
927 long groupId, boolean privateLayout, String url)
928 throws PortalException, SystemException {
929
930 return getPortletFriendlyURLMapper(groupId, privateLayout, url, null);
931 }
932
933 public static Object[] getPortletFriendlyURLMapper(
934 long groupId, boolean privateLayout, String url, Map params)
935 throws PortalException, SystemException {
936
937 String friendlyURL = url;
938 String queryString = StringPool.BLANK;
939
940 List friendlyURLMappers =
941 PortletLocalServiceUtil.getFriendlyURLMappers();
942
943 Iterator itr = friendlyURLMappers.iterator();
944
945 while (itr.hasNext()) {
946 FriendlyURLMapper friendlyURLMapper = (FriendlyURLMapper)itr.next();
947
948 if (url.endsWith(
949 StringPool.SLASH + friendlyURLMapper.getMapping())) {
950
951 url += StringPool.SLASH;
952 }
953
954 int pos = url.indexOf(
955 StringPool.SLASH + friendlyURLMapper.getMapping() +
956 StringPool.SLASH);
957
958 if (pos != -1) {
959 friendlyURL = url.substring(0, pos);
960
961 Map actualParams = null;
962
963 if (params != null) {
964 actualParams = new HashMap(params);
965 }
966 else {
967 actualParams = new HashMap();
968 }
969
970
982
983 friendlyURLMapper.populateParams(
984 url.substring(pos), actualParams);
985
986 queryString =
987 StringPool.AMPERSAND +
988 HttpUtil.parameterMapToString(actualParams, false);
989
990 break;
991 }
992 }
993
994 friendlyURL = StringUtil.replace(
995 friendlyURL, StringPool.DOUBLE_SLASH, StringPool.SLASH);
996
997 if (friendlyURL.endsWith(StringPool.SLASH)) {
998 friendlyURL = friendlyURL.substring(0, friendlyURL.length() - 1);
999 }
1000
1001 Layout layout = null;
1002
1003 try {
1004 layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
1005 groupId, privateLayout, friendlyURL);
1006 }
1007 catch(NoSuchLayoutException nsle) {
1008 long layoutId = GetterUtil.getLong(friendlyURL.substring(1));
1009
1010 layout = LayoutLocalServiceUtil.getLayout(
1011 groupId, privateLayout, layoutId);
1012 }
1013
1014 return new Object[] {layout, queryString};
1015 }
1016
1017 public static long getPortletGroupId(long plid) {
1018 Layout layout = null;
1019
1020 try {
1021 layout = LayoutLocalServiceUtil.getLayout(plid);
1022 }
1023 catch (Exception e) {
1024 }
1025
1026 return getPortletGroupId(layout);
1027 }
1028
1029 public static long getPortletGroupId(Layout layout) {
1030 if (layout == null) {
1031 return 0;
1032 }
1033 else {
1034 return layout.getGroupId();
1035 }
1036 }
1037
1038 public static long getPortletGroupId(HttpServletRequest req) {
1039 Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
1040
1041 return getPortletGroupId(layout);
1042 }
1043
1044 public static long getPortletGroupId(ActionRequest req) {
1045 return getPortletGroupId(getHttpServletRequest(req));
1046 }
1047
1048 public static long getPortletGroupId(RenderRequest req) {
1049 return getPortletGroupId(getHttpServletRequest(req));
1050 }
1051
1052 public static String getPortletNamespace(String portletId) {
1053 StringMaker sm = new StringMaker();
1054
1055 sm.append(StringPool.UNDERLINE);
1056 sm.append(portletId);
1057 sm.append(StringPool.UNDERLINE);
1058
1059 return sm.toString();
1060 }
1061
1062 public static String getPortletXmlFileName()
1063 throws PortalException, SystemException {
1064
1065 if (PrefsPropsUtil.getBoolean(
1066 PropsUtil.AUTO_DEPLOY_CUSTOM_PORTLET_XML)) {
1067
1068 return PORTLET_XML_FILE_NAME_CUSTOM;
1069 }
1070 else {
1071 return PORTLET_XML_FILE_NAME_STANDARD;
1072 }
1073 }
1074
1075 public static String getPortletTitle(
1076 String portletId, long companyId, String languageId) {
1077
1078 Locale locale = LocaleUtil.fromLanguageId(languageId);
1079
1080 StringMaker sm = new StringMaker();
1081
1082 sm.append(JavaConstants.JAVAX_PORTLET_TITLE);
1083 sm.append(StringPool.PERIOD);
1084 sm.append(portletId);
1085
1086 return LanguageUtil.get(companyId, locale, sm.toString());
1087 }
1088
1089 public static String getPortletTitle(String portletId, User user) {
1090 StringMaker sm = new StringMaker();
1091
1092 sm.append(JavaConstants.JAVAX_PORTLET_TITLE);
1093 sm.append(StringPool.PERIOD);
1094 sm.append(portletId);
1095
1096 return LanguageUtil.get(
1097 user.getCompanyId(), user.getLocale(), sm.toString());
1098 }
1099
1100 public static String getPortletTitle(
1101 Portlet portlet, ServletContext ctx, Locale locale) {
1102
1103 PortletConfig portletConfig = PortletConfigFactory.create(portlet, ctx);
1104
1105 ResourceBundle resourceBundle = portletConfig.getResourceBundle(locale);
1106
1107 return resourceBundle.getString(JavaConstants.JAVAX_PORTLET_TITLE);
1108 }
1109
1110 public static PortletPreferences getPreferences(HttpServletRequest req) {
1111 RenderRequest renderRequest = (RenderRequest)req.getAttribute(
1112 JavaConstants.JAVAX_PORTLET_REQUEST);
1113
1114 PortletPreferences prefs = null;
1115
1116 if (renderRequest != null) {
1117 PortletPreferencesWrapper prefsWrapper =
1118 (PortletPreferencesWrapper)renderRequest.getPreferences();
1119
1120 prefs = prefsWrapper.getPreferencesImpl();
1121 }
1122
1123 return prefs;
1124 }
1125
1126 public static PreferencesValidator getPreferencesValidator(
1127 Portlet portlet) {
1128
1129 if (portlet.isWARFile()) {
1130 PortletContextWrapper pcw =
1131 PortletContextPool.get(portlet.getRootPortletId());
1132
1133 return pcw.getPreferencesValidatorInstance();
1134 }
1135 else {
1136 PreferencesValidator prefsValidator = null;
1137
1138 if (Validator.isNotNull(portlet.getPreferencesValidator())) {
1139 prefsValidator =
1140 (PreferencesValidator)InstancePool.get(
1141 portlet.getPreferencesValidator());
1142 }
1143
1144 return prefsValidator;
1145 }
1146 }
1147
1148 public static User getSelectedUser(HttpServletRequest req)
1149 throws PortalException, RemoteException, SystemException {
1150
1151 return getSelectedUser(req, true);
1152 }
1153
1154 public static User getSelectedUser(
1155 HttpServletRequest req, boolean checkPermission)
1156 throws PortalException, RemoteException, SystemException {
1157
1158 long userId = ParamUtil.getLong(req, "p_u_i_d");
1159
1160 User user = null;
1161
1162 try {
1163 if (checkPermission) {
1164 user = UserServiceUtil.getUserById(userId);
1165 }
1166 else {
1167 user = UserLocalServiceUtil.getUserById(userId);
1168 }
1169 }
1170 catch (NoSuchUserException nsue) {
1171 }
1172
1173 return user;
1174 }
1175
1176 public static User getSelectedUser(ActionRequest req)
1177 throws PortalException, RemoteException, SystemException {
1178
1179 return getSelectedUser(req, true);
1180 }
1181
1182 public static User getSelectedUser(
1183 ActionRequest req, boolean checkPermission)
1184 throws PortalException, RemoteException, SystemException {
1185
1186 return getSelectedUser(getHttpServletRequest(req), checkPermission);
1187 }
1188
1189 public static User getSelectedUser(RenderRequest req)
1190 throws PortalException, RemoteException, SystemException {
1191
1192 return getSelectedUser(req, true);
1193 }
1194
1195 public static User getSelectedUser(
1196 RenderRequest req, boolean checkPermission)
1197 throws PortalException, RemoteException, SystemException {
1198
1199 return getSelectedUser(getHttpServletRequest(req), checkPermission);
1200 }
1201
1202 public static String[] getSystemCommunityRoles() {
1203 return _instance._getSystemCommunityRoles();
1204 }
1205
1206 public static String[] getSystemGroups() {
1207 return _instance._getSystemGroups();
1208 }
1209
1210 public static String[] getSystemRoles() {
1211 return _instance._getSystemRoles();
1212 }
1213
1214 public static UploadPortletRequest getUploadPortletRequest(
1215 ActionRequest req) {
1216
1217 ActionRequestImpl actionReq = (ActionRequestImpl)req;
1218
1219 DynamicServletRequest dynamicReq =
1220 (DynamicServletRequest)actionReq.getHttpServletRequest();
1221
1222 HttpServletRequestWrapper reqWrapper =
1223 (HttpServletRequestWrapper)dynamicReq.getRequest();
1224
1225 UploadServletRequest uploadReq = getUploadServletRequest(reqWrapper);
1226
1227 return new UploadPortletRequest(
1228 uploadReq, getPortletNamespace(actionReq.getPortletName()));
1229 }
1230
1231 public static UploadServletRequest getUploadServletRequest(
1232 HttpServletRequest httpReq) {
1233
1234 HttpServletRequestWrapper httpReqWrapper = null;
1235
1236 if (httpReq instanceof HttpServletRequestWrapper) {
1237 httpReqWrapper = (HttpServletRequestWrapper)httpReq;
1238 }
1239
1240 UploadServletRequest uploadReq = null;
1241
1242 while (uploadReq == null) {
1243
1244
1247 if (httpReqWrapper instanceof UploadServletRequest) {
1248 uploadReq = (UploadServletRequest)httpReqWrapper;
1249 }
1250 else {
1251 ServletRequest req = httpReqWrapper.getRequest();
1252
1253 if (!(req instanceof HttpServletRequestWrapper)) {
1254 break;
1255 }
1256 else {
1257 httpReqWrapper =
1258 (HttpServletRequestWrapper)httpReqWrapper.getRequest();
1259 }
1260 }
1261 }
1262
1263 return uploadReq;
1264 }
1265
1266 public static Date getUptime() {
1267 return UP_TIME;
1268 }
1269
1270 public static User getUser(HttpServletRequest req)
1271 throws PortalException, SystemException {
1272
1273 long userId = getUserId(req);
1274
1275 if (userId <= 0) {
1276
1277
1282 String remoteUser = req.getRemoteUser();
1283
1284 if (remoteUser == null) {
1285 return null;
1286 }
1287
1288 userId = GetterUtil.getLong(remoteUser);
1289 }
1290
1291 User user = (User)req.getAttribute(WebKeys.USER);
1292
1293 if (user == null) {
1294 user = UserLocalServiceUtil.getUserById(userId);
1295
1296 req.setAttribute(WebKeys.USER, user);
1297 }
1298
1299 return user;
1300 }
1301
1302 public static User getUser(ActionRequest req)
1303 throws PortalException, SystemException {
1304
1305 return getUser(getHttpServletRequest(req));
1306 }
1307
1308 public static User getUser(RenderRequest req)
1309 throws PortalException, SystemException {
1310
1311 return getUser(getHttpServletRequest(req));
1312 }
1313
1314 public static long getUserId(HttpServletRequest req) {
1315 Long userIdObj = (Long)req.getAttribute(WebKeys.USER_ID);
1316
1317 if (userIdObj != null) {
1318 return userIdObj.longValue();
1319 }
1320
1321 if (!GetterUtil.getBoolean(
1322 PropsUtil.get(PropsUtil.PORTAL_JAAS_ENABLE)) &&
1323 GetterUtil.getBoolean(
1324 PropsUtil.get(PropsUtil.PORTAL_IMPERSONATION_ENABLE))) {
1325
1326 String doAsUserIdString = ParamUtil.getString(req, "doAsUserId");
1327
1328 try {
1329 long doAsUserId = _getDoAsUserId(req, doAsUserIdString);
1330
1331 if (doAsUserId > 0) {
1332 if (_log.isDebugEnabled()) {
1333 _log.debug("Impersonating user " + doAsUserId);
1334 }
1335
1336 return doAsUserId;
1337 }
1338 }
1339 catch (Exception e) {
1340 _log.error("Unable to impersonate user " + doAsUserIdString, e);
1341 }
1342 }
1343
1344 HttpSession ses = req.getSession();
1345
1346 userIdObj = (Long)ses.getAttribute(WebKeys.USER_ID);
1347
1348 if (userIdObj != null) {
1349 req.setAttribute(WebKeys.USER_ID, userIdObj);
1350
1351 return userIdObj.longValue();
1352 }
1353 else {
1354 return 0;
1355 }
1356 }
1357
1358 public static long getUserId(ActionRequest req) {
1359 return getUserId(getHttpServletRequest(req));
1360 }
1361
1362 public static long getUserId(RenderRequest req) {
1363 return getUserId(getHttpServletRequest(req));
1364 }
1365
1366 public static String getUserName(long userId, String defaultUserName) {
1367 return getUserName(
1368 userId, defaultUserName, UserAttributes.USER_NAME_FULL);
1369 }
1370
1371 public static String getUserName(
1372 long userId, String defaultUserName, String userAttribute) {
1373
1374 return getUserName(userId, defaultUserName, userAttribute, null);
1375 }
1376
1377 public static String getUserName(
1378 long userId, String defaultUserName, HttpServletRequest req) {
1379
1380 return getUserName(
1381 userId, defaultUserName, UserAttributes.USER_NAME_FULL, req);
1382 }
1383
1384 public static String getUserName(
1385 long userId, String defaultUserName, String userAttribute,
1386 HttpServletRequest req) {
1387
1388 String userName = defaultUserName;
1389
1390 try {
1391 User user = UserLocalServiceUtil.getUserById(userId);
1392
1393 if (userAttribute.equals(UserAttributes.USER_NAME_FULL)) {
1394 userName = user.getFullName();
1395 }
1396 else {
1397 userName = user.getScreenName();
1398 }
1399
1400 if (req != null) {
1401 Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
1402
1403 PortletURL portletURL = new PortletURLImpl(
1404 req, PortletKeys.DIRECTORY, layout.getPlid(), false);
1405
1406 portletURL.setWindowState(WindowState.MAXIMIZED);
1407 portletURL.setPortletMode(PortletMode.VIEW);
1408
1409 portletURL.setParameter("struts_action", "/directory/edit_user");
1410 portletURL.setParameter(
1411 "p_u_i_d", String.valueOf(user.getUserId()));
1412
1413 userName =
1414 "<a href=\"" + portletURL.toString() + "\">" + userName +
1415 "</a>";
1416 }
1417 }
1418 catch (Exception e) {
1419 }
1420
1421 return userName;
1422 }
1423
1424 public static String getUserPassword(HttpSession ses) {
1425 return (String)ses.getAttribute(WebKeys.USER_PASSWORD);
1426 }
1427
1428 public static String getUserPassword(HttpServletRequest req) {
1429 return getUserPassword(req.getSession());
1430 }
1431
1432 public static String getUserPassword(ActionRequest req) {
1433 return getUserPassword(getHttpServletRequest(req));
1434 }
1435
1436 public static String getUserPassword(RenderRequest req) {
1437 return getUserPassword(getHttpServletRequest(req));
1438 }
1439
1440 public static boolean isLayoutFriendliable(Layout layout) {
1441 return PropsUtil.getComponentProperties().getBoolean(
1442 PropsUtil.LAYOUT_URL_FRIENDLIABLE,
1443 Filter.by(layout.getType()), true);
1444 }
1445
1446 public static boolean isLayoutParentable(Layout layout) {
1447 return isLayoutParentable(layout.getType());
1448 }
1449
1450 public static boolean isLayoutParentable(String type) {
1451 return PropsUtil.getComponentProperties().getBoolean(
1452 PropsUtil.LAYOUT_PARENTABLE, Filter.by(type), true);
1453 }
1454
1455 public static boolean isLayoutSitemapable(Layout layout) {
1456 if (layout.isPrivateLayout()) {
1457 return false;
1458 }
1459
1460 return PropsUtil.getComponentProperties().getBoolean(
1461 PropsUtil.LAYOUT_SITEMAPABLE, Filter.by(layout.getType()), true);
1462 }
1463
1464 public static boolean isReservedParameter(String name) {
1465 return _instance._reservedParams.contains(name);
1466 }
1467
1468 public static boolean isSystemGroup(String groupName) {
1469 return _instance._isSystemGroup(groupName);
1470 }
1471
1472 public static boolean isSystemRole(String roleName) {
1473 return _instance._isSystemRole(roleName);
1474 }
1475
1476 public static boolean isUpdateAvailable()
1477 throws PortalException, SystemException {
1478
1479 return PluginPackageUtil.isUpdateAvailable();
1480 }
1481
1482 public static Document readDocumentFromFile(File file)
1483 throws DocumentException {
1484
1485 return readDocumentFromFile(file, false);
1486 }
1487
1488 public static Document readDocumentFromFile(File file, boolean validate)
1489 throws DocumentException {
1490
1491 SAXReader reader = SAXReaderFactory.getInstance(validate);
1492
1493 return reader.read(file);
1494 }
1495
1496 public static Document readDocumentFromStream(InputStream is)
1497 throws DocumentException {
1498
1499 return readDocumentFromStream(is, false);
1500 }
1501
1502 public static Document readDocumentFromStream(
1503 InputStream is, boolean validate)
1504 throws DocumentException {
1505
1506 SAXReader reader = SAXReaderFactory.getInstance(validate);
1507
1508 return reader.read(is);
1509 }
1510
1511 public static Document readDocumentFromURL(String url)
1512 throws DocumentException, IOException {
1513
1514 return readDocumentFromURL(url, false);
1515 }
1516
1517 public static Document readDocumentFromURL(String url, boolean validate)
1518 throws DocumentException, IOException {
1519
1520 SAXReader reader = SAXReaderFactory.getInstance(validate);
1521
1522 return reader.read(new URL(url));
1523 }
1524
1525 public static Document readDocumentFromXML(String xml)
1526 throws DocumentException {
1527
1528 return readDocumentFromXML(xml, false);
1529 }
1530
1531 public static Document readDocumentFromXML(String xml, boolean validate)
1532 throws DocumentException {
1533
1534 SAXReader reader = SAXReaderFactory.getInstance(validate);
1535
1536 return reader.read(new XMLSafeReader(xml));
1537 }
1538
1539 public static void renderPage(
1540 StringMaker sm, ServletContext ctx, HttpServletRequest req,
1541 HttpServletResponse res, String path)
1542 throws IOException, ServletException {
1543
1544 RequestDispatcher rd = ctx.getRequestDispatcher(path);
1545
1546 StringServletResponse stringServletRes =
1547 new StringServletResponse(res);
1548
1549 rd.include(req, stringServletRes);
1550
1551 sm.append(stringServletRes.getString());
1552 }
1553
1554 public static void renderPortlet(
1555 StringMaker sm, ServletContext ctx, HttpServletRequest req,
1556 HttpServletResponse res, Portlet portlet, String queryString)
1557 throws IOException, ServletException {
1558
1559 renderPortlet(
1560 sm, ctx, req, res, portlet, queryString, null, null, null);
1561 }
1562
1563 public static void renderPortlet(
1564 StringMaker sm, ServletContext ctx, HttpServletRequest req,
1565 HttpServletResponse res, Portlet portlet, String queryString,
1566 String columnId, Integer columnPos, Integer columnCount)
1567 throws IOException, ServletException {
1568
1569 renderPortlet(
1570 sm, ctx, req, res, portlet, queryString, columnId, columnPos,
1571 columnCount, null);
1572 }
1573
1574 public static void renderPortlet(
1575 StringMaker sm, ServletContext ctx, HttpServletRequest req,
1576 HttpServletResponse res, Portlet portlet, String queryString,
1577 String columnId, Integer columnPos, Integer columnCount,
1578 String path)
1579 throws IOException, ServletException {
1580
1581 queryString = GetterUtil.getString(queryString);
1582 columnId = GetterUtil.getString(columnId);
1583
1584 if (columnPos == null) {
1585 columnPos = new Integer(0);
1586 }
1587
1588 if (columnCount == null) {
1589 columnCount = new Integer(0);
1590 }
1591
1592 req.setAttribute(WebKeys.RENDER_PORTLET, portlet);
1593 req.setAttribute(WebKeys.RENDER_PORTLET_QUERY_STRING, queryString);
1594 req.setAttribute(WebKeys.RENDER_PORTLET_COLUMN_ID, columnId);
1595 req.setAttribute(WebKeys.RENDER_PORTLET_COLUMN_POS, columnPos);
1596 req.setAttribute(WebKeys.RENDER_PORTLET_COLUMN_COUNT, columnCount);
1597
1598 if (path == null) {
1599 path = "/html/portal/render_portlet.jsp";
1600 }
1601
1602 RequestDispatcher rd = ctx.getRequestDispatcher(path);
1603
1604 if (sm != null) {
1605 StringServletResponse stringServletRes =
1606 new StringServletResponse(res);
1607
1608 rd.include(req, stringServletRes);
1609
1610 sm.append(stringServletRes.getString());
1611 }
1612 else {
1613
1614
1616 res.setContentType(ContentTypes.TEXT_HTML_UTF8);
1617
1618 rd.include(req, res);
1619 }
1620 }
1621
1622
1629 public static void setPageSubtitle(
1630 String subtitle, HttpServletRequest req) {
1631
1632 req.setAttribute(WebKeys.PAGE_SUBTITLE, subtitle);
1633 }
1634
1635
1642 public static void setPageTitle(String title, HttpServletRequest req) {
1643 req.setAttribute(WebKeys.PAGE_TITLE, title);
1644 }
1645
1646 public static void storePreferences(PortletPreferences prefs)
1647 throws IOException, ValidatorException {
1648
1649 PortletPreferencesWrapper prefsWrapper =
1650 (PortletPreferencesWrapper)prefs;
1651
1652 PortletPreferencesImpl prefsImpl =
1653 (PortletPreferencesImpl)prefsWrapper.getPreferencesImpl();
1654
1655 prefsImpl.store();
1656 }
1657
1658 public static PortletMode updatePortletMode(
1659 String portletId, User user, Layout layout, PortletMode portletMode,
1660 HttpServletRequest req)
1661 throws PortalException, RemoteException, SystemException {
1662
1663 LayoutTypePortlet layoutType =
1664 (LayoutTypePortlet)layout.getLayoutType();
1665
1666 if (portletMode == null || Validator.isNull(portletMode.toString())) {
1667 if (layoutType.hasModeAboutPortletId(portletId)) {
1668 return LiferayPortletMode.ABOUT;
1669 }
1670 else if (layoutType.hasModeConfigPortletId(portletId)) {
1671 return LiferayPortletMode.CONFIG;
1672 }
1673 else if (layoutType.hasModeEditPortletId(portletId)) {
1674 return PortletMode.EDIT;
1675 }
1676 else if (layoutType.hasModeEditDefaultsPortletId(portletId)) {
1677 return LiferayPortletMode.EDIT_DEFAULTS;
1678 }
1679 else if (layoutType.hasModeEditGuestPortletId(portletId)) {
1680 return LiferayPortletMode.EDIT_GUEST;
1681 }
1682 else if (layoutType.hasModeHelpPortletId(portletId)) {
1683 return PortletMode.HELP;
1684 }
1685 else if (layoutType.hasModePreviewPortletId(portletId)) {
1686 return LiferayPortletMode.PREVIEW;
1687 }
1688 else if (layoutType.hasModePrintPortletId(portletId)) {
1689 return LiferayPortletMode.PRINT;
1690 }
1691 else {
1692 return PortletMode.VIEW;
1693 }
1694 }
1695 else {
1696 boolean updateLayout = false;
1697
1698 if (portletMode.equals(LiferayPortletMode.ABOUT) &&
1699 !layoutType.hasModeAboutPortletId(portletId)) {
1700
1701 layoutType.addModeAboutPortletId(portletId);
1702
1703 updateLayout = true;
1704 }
1705 else if (portletMode.equals(LiferayPortletMode.CONFIG) &&
1706 !layoutType.hasModeConfigPortletId(portletId)) {
1707
1708 layoutType.addModeConfigPortletId(portletId);
1709
1710 updateLayout = true;
1711 }
1712 else if (portletMode.equals(PortletMode.EDIT) &&
1713 !layoutType.hasModeEditPortletId(portletId)) {
1714
1715 layoutType.addModeEditPortletId(portletId);
1716
1717 updateLayout = true;
1718 }
1719 else if (portletMode.equals(LiferayPortletMode.EDIT_DEFAULTS) &&
1720 !layoutType.hasModeEditDefaultsPortletId(portletId)) {
1721
1722 layoutType.addModeEditDefaultsPortletId(portletId);
1723
1724 updateLayout = true;
1725 }
1726 else if (portletMode.equals(LiferayPortletMode.EDIT_GUEST) &&
1727 !layoutType.hasModeEditGuestPortletId(portletId)) {
1728
1729 layoutType.addModeEditGuestPortletId(portletId);
1730
1731 updateLayout = true;
1732 }
1733 else if (portletMode.equals(PortletMode.HELP) &&
1734 !layoutType.hasModeHelpPortletId(portletId)) {
1735
1736 layoutType.addModeHelpPortletId(portletId);
1737
1738 updateLayout = true;
1739 }
1740 else if (portletMode.equals(LiferayPortletMode.PREVIEW) &&
1741 !layoutType.hasModePreviewPortletId(portletId)) {
1742
1743 layoutType.addModePreviewPortletId(portletId);
1744
1745 updateLayout = true;
1746 }
1747 else if (portletMode.equals(LiferayPortletMode.PRINT) &&
1748 !layoutType.hasModePrintPortletId(portletId)) {
1749
1750 layoutType.addModePrintPortletId(portletId);
1751
1752 updateLayout = true;
1753 }
1754 else if (portletMode.equals(PortletMode.VIEW) &&
1755 !layoutType.hasModeViewPortletId(portletId)) {
1756
1757 layoutType.removeModesPortletId(portletId);
1758
1759 updateLayout = true;
1760 }
1761
1762 if (updateLayout) {
1763 LayoutClone layoutClone = LayoutCloneFactory.getInstance();
1764
1765 if (layoutClone != null) {
1766 layoutClone.update(
1767 req, layout.getPlid(), layout.getTypeSettings());
1768 }
1769 }
1770
1771 return portletMode;
1772 }
1773 }
1774
1775 public static WindowState updateWindowState(
1776 String portletId, User user, Layout layout, WindowState windowState,
1777 HttpServletRequest req)
1778 throws PortalException, RemoteException, SystemException {
1779
1780 LayoutTypePortlet layoutType =
1781 (LayoutTypePortlet)layout.getLayoutType();
1782
1783 if ((windowState == null) ||
1784 (Validator.isNull(windowState.toString()))) {
1785
1786 if (layoutType.hasStateMaxPortletId(portletId)) {
1787 return WindowState.MAXIMIZED;
1788 }
1789 else if (layoutType.hasStateMinPortletId(portletId)) {
1790 return WindowState.MINIMIZED;
1791 }
1792 else {
1793 return WindowState.NORMAL;
1794 }
1795 }
1796 else {
1797 boolean updateLayout = false;
1798
1799 if ((windowState.equals(WindowState.MAXIMIZED)) ||
1800 (windowState.equals(LiferayWindowState.EXCLUSIVE)) ||
1801 (windowState.equals(LiferayWindowState.POP_UP))) {
1802
1803 if (layoutType.hasStateMax()) {
1804 String curMaxPortletId =
1805 StringUtil.split(layoutType.getStateMax())[0];
1806
1807
1810 CachePortlet.clearResponse(
1811 req.getSession(), layout.getPlid(), curMaxPortletId,
1812 LanguageUtil.getLanguageId(req));
1813
1814
1816
1817 if ((windowState.equals(LiferayWindowState.EXCLUSIVE)) ||
1818 (windowState.equals(LiferayWindowState.POP_UP))) {
1819
1820 String stateMaxPrevious =
1821 layoutType.getStateMaxPrevious();
1822
1823 if (stateMaxPrevious == null) {
1824 layoutType.setStateMaxPrevious(curMaxPortletId);
1825
1826 updateLayout = true;
1827 }
1828 }
1829 }
1830 else {
1831 if ((windowState.equals(LiferayWindowState.EXCLUSIVE)) ||
1832 (windowState.equals(LiferayWindowState.POP_UP))) {
1833
1834 String stateMaxPrevious =
1835 layoutType.getStateMaxPrevious();
1836
1837 if (stateMaxPrevious == null) {
1838 layoutType.setStateMaxPrevious(StringPool.BLANK);
1839
1840 updateLayout = true;
1841 }
1842 }
1843 }
1844
1845 if (!layoutType.hasStateMaxPortletId(portletId)) {
1846 layoutType.addStateMaxPortletId(portletId);
1847
1848 updateLayout = true;
1849 }
1850 }
1851 else if (windowState.equals(WindowState.MINIMIZED) &&
1852 !layoutType.hasStateMinPortletId(portletId)) {
1853
1854 layoutType.addStateMinPortletId(portletId);
1855
1856 updateLayout = true;
1857 }
1858 else if (windowState.equals(WindowState.NORMAL) &&
1859 !layoutType.hasStateNormalPortletId(portletId)) {
1860
1861 layoutType.removeStatesPortletId(portletId);
1862
1863 updateLayout = true;
1864 }
1865
1866 if (updateLayout) {
1867 LayoutClone layoutClone = LayoutCloneFactory.getInstance();
1868
1869 if (layoutClone != null) {
1870 layoutClone.update(
1871 req, layout.getPlid(), layout.getTypeSettings());
1872 }
1873 }
1874
1875 return windowState;
1876 }
1877 }
1878
1879 private static long _getDoAsUserId(
1880 HttpServletRequest req, String doAsUserIdString)
1881 throws Exception {
1882
1883 if (Validator.isNull(doAsUserIdString)) {
1884 return 0;
1885 }
1886
1887 long doAsUserId = 0;
1888
1889 try {
1890 Company company = getCompany(req);
1891
1892 doAsUserId = GetterUtil.getLong(
1893 Encryptor.decrypt(company.getKeyObj(), doAsUserIdString));
1894 }
1895 catch (Exception e) {
1896 if (_log.isWarnEnabled()) {
1897 _log.warn(
1898 "Unable to impersonate " + doAsUserIdString +
1899 " because the string cannot be decrypted",
1900 e);
1901 }
1902
1903 return 0;
1904 }
1905
1906 String path = GetterUtil.getString(req.getPathInfo());
1907
1908 boolean alwaysAllowDoAsUser = false;
1909
1910 if (path.equals("/portal/fckeditor")) {
1911 alwaysAllowDoAsUser = true;
1912 }
1913
1914 if (alwaysAllowDoAsUser) {
1915 req.setAttribute(WebKeys.USER_ID, new Long(doAsUserId));
1916
1917 return doAsUserId;
1918 }
1919
1920 HttpSession ses = req.getSession();
1921
1922 Long realUserIdObj = (Long)ses.getAttribute(WebKeys.USER_ID);
1923
1924 if (realUserIdObj == null) {
1925 return 0;
1926 }
1927
1928 User doAsUser = UserLocalServiceUtil.getUserById(doAsUserId);
1929
1930 long organizationId = doAsUser.getOrganization().getOrganizationId();
1931 long locationId = doAsUser.getLocation().getOrganizationId();
1932
1933 User realUser = UserLocalServiceUtil.getUserById(
1934 realUserIdObj.longValue());
1935 boolean checkGuest = true;
1936
1937 PermissionCheckerImpl permissionChecker = null;
1938
1939 try {
1940 permissionChecker = PermissionCheckerFactory.create(
1941 realUser, checkGuest);
1942
1943 if (doAsUser.isDefaultUser() ||
1944 UserPermissionUtil.contains(
1945 permissionChecker, doAsUserId, organizationId, locationId,
1946 ActionKeys.IMPERSONATE)) {
1947
1948 req.setAttribute(WebKeys.USER_ID, new Long(doAsUserId));
1949
1950 return doAsUserId;
1951 }
1952 else {
1953 _log.error(
1954 "User " + realUserIdObj + " does not have the permission " +
1955 "to impersonate " + doAsUserId);
1956
1957 return 0;
1958 }
1959 }
1960 finally {
1961 try {
1962 PermissionCheckerFactory.recycle(permissionChecker);
1963 }
1964 catch (Exception e) {
1965 }
1966 }
1967 }
1968
1969 private PortalUtil() {
1970
1971
1973 ClassLoader classLoader = getClass().getClassLoader();
1974
1975 URL url = classLoader.getResource(
1976 "com/liferay/portal/util/PortalUtil.class");
1977
1978 String file = url.getFile();
1979
1980 if (_log.isInfoEnabled()) {
1981 _log.info("Portal lib url " + file);
1982 }
1983
1984 int pos = file.indexOf("/com/liferay/portal/util/");
1985
1986 _portalLibDir = file.substring(0, pos + 1);
1987
1988 if (_portalLibDir.endsWith("/WEB-INF/classes/")) {
1989 _portalLibDir = _portalLibDir.substring(
1990 0, _portalLibDir.length() - 8) + "lib/";
1991 }
1992 else {
1993 pos = _portalLibDir.indexOf("/WEB-INF/lib/");
1994
1995 if (pos != -1) {
1996 _portalLibDir =
1997 _portalLibDir.substring(0, pos) + "/WEB-INF/lib/";
1998 }
1999 }
2000
2001 if (_portalLibDir.startsWith("file:/")) {
2002 _portalLibDir = _portalLibDir.substring(5, _portalLibDir.length());
2003 }
2004
2005 if (_log.isInfoEnabled()) {
2006 _log.info("Portal lib directory " + _portalLibDir);
2007 }
2008
2009
2011 _cdnHost = PropsUtil.get(PropsUtil.CDN_HOST);
2012
2013
2015 _pathContext = PropsUtil.get(PropsUtil.PORTAL_CTX);
2016
2017 if (_pathContext.equals(StringPool.SLASH)) {
2018 _pathContext = StringPool.BLANK;
2019 }
2020
2021 _pathFriendlyURLPrivateGroup = _pathContext + PropsUtil.get(
2022 PropsUtil.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING);
2023 _pathFriendlyURLPrivateUser = _pathContext + PropsUtil.get(
2024 PropsUtil.LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING);
2025 _pathFriendlyURLPublic = _pathContext + PropsUtil.get(
2026 PropsUtil.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING);
2027 _pathImage = _cdnHost + _pathContext + PATH_IMAGE;
2028 _pathMain = _pathContext + PATH_MAIN;
2029
2030
2032 String customSystemGroups[] =
2033 PropsUtil.getArray(PropsUtil.SYSTEM_GROUPS);
2034
2035 if ((customSystemGroups == null) || (customSystemGroups.length == 0)) {
2036 _allSystemGroups = GroupImpl.SYSTEM_GROUPS;
2037 }
2038 else {
2039 _allSystemGroups = ArrayUtil.append(
2040 GroupImpl.SYSTEM_GROUPS, customSystemGroups);
2041 }
2042
2043 _sortedSystemGroups = new String[_allSystemGroups.length];
2044
2045 System.arraycopy(
2046 _allSystemGroups, 0, _sortedSystemGroups, 0,
2047 _allSystemGroups.length);
2048
2049 Arrays.sort(_sortedSystemGroups, new StringComparator());
2050
2051
2053 String customSystemRoles[] = PropsUtil.getArray(PropsUtil.SYSTEM_ROLES);
2054
2055 if ((customSystemRoles == null) || (customSystemRoles.length == 0)) {
2056 _allSystemRoles = RoleImpl.SYSTEM_ROLES;
2057 }
2058 else {
2059 _allSystemRoles = ArrayUtil.append(
2060 RoleImpl.SYSTEM_ROLES, customSystemRoles);
2061 }
2062
2063 _sortedSystemRoles = new String[_allSystemRoles.length];
2064
2065 System.arraycopy(
2066 _allSystemRoles, 0, _sortedSystemRoles, 0, _allSystemRoles.length);
2067
2068 Arrays.sort(_sortedSystemRoles, new StringComparator());
2069
2070
2072 String customSystemCommunityRoles[] =
2073 PropsUtil.getArray(PropsUtil.SYSTEM_COMMUNITY_ROLES);
2074
2075 if ((customSystemCommunityRoles == null) ||
2076 (customSystemCommunityRoles.length == 0)) {
2077
2078 _allSystemCommunityRoles = RoleImpl.SYSTEM_COMMUNITY_ROLES;
2079 }
2080 else {
2081 _allSystemCommunityRoles = ArrayUtil.append(
2082 RoleImpl.SYSTEM_COMMUNITY_ROLES, customSystemCommunityRoles);
2083 }
2084
2085 _sortedSystemCommunityRoles =
2086 new String[_allSystemCommunityRoles.length];
2087
2088 System.arraycopy(
2089 _allSystemCommunityRoles, 0, _sortedSystemCommunityRoles, 0,
2090 _allSystemCommunityRoles.length);
2091
2092 Arrays.sort(_sortedSystemCommunityRoles, new StringComparator());
2093
2094
2096 _reservedParams = CollectionFactory.getHashSet();
2097
2098 _reservedParams.add("p_l_id");
2099 _reservedParams.add("p_l_reset");
2100 _reservedParams.add("p_p_id");
2101 _reservedParams.add("p_p_action");
2102 _reservedParams.add("p_p_state");
2103 _reservedParams.add("p_p_mode");
2104 _reservedParams.add("p_p_width");
2105 _reservedParams.add("p_p_col_id");
2106 _reservedParams.add("p_p_col_pos");
2107 _reservedParams.add("p_p_col_count");
2108 _reservedParams.add("p_p_static");
2109 }
2110
2111 private String _getCDNHost() {
2112 return _cdnHost;
2113 }
2114
2115 private static String _getCurrentURL(HttpServletRequest req) {
2116 StringMaker sm = new StringMaker();
2117
2118 StringBuffer requestURL = req.getRequestURL();
2119
2120 if (requestURL != null) {
2121 sm.append(requestURL.toString());
2122 }
2123
2124 String queryString = req.getQueryString();
2125
2126 if (Validator.isNull(queryString)) {
2127 return sm.toString();
2128 }
2129
2130 String portletId = req.getParameter("p_p_id");
2131
2132 String redirectParam = "redirect";
2133
2134 if (Validator.isNotNull(portletId)) {
2135 redirectParam = getPortletNamespace(portletId) + redirectParam;
2136 }
2137
2138 Map parameterMap = HttpUtil.parameterMapFromString(queryString);
2139
2140 String[] redirectValues = (String[])parameterMap.get(redirectParam);
2141
2142 if ((redirectValues != null) && (redirectValues.length > 0)) {
2143
2144
2148 String redirect = HttpUtil.decodeURL(
2149 GetterUtil.getString(redirectValues[0]));
2150
2151 int pos = redirect.indexOf(StringPool.QUESTION);
2152
2153 if (pos != -1) {
2154 String subqueryString = redirect.substring(
2155 pos + 1, redirect.length());
2156
2157 Map subparameterMap = HttpUtil.parameterMapFromString(
2158 subqueryString);
2159
2160 String[] subredirectValues = (String[])subparameterMap.get(
2161 redirectParam);
2162
2163 if ((subredirectValues != null) &&
2164 (subredirectValues.length > 0)) {
2165
2166 String subredirect = HttpUtil.decodeURL(
2167 GetterUtil.getString(subredirectValues[0]));
2168
2169 parameterMap.put(redirectParam, new String[] {subredirect});
2170
2171 queryString = HttpUtil.parameterMapToString(
2172 parameterMap, false);
2173 }
2174 }
2175 }
2176
2177 sm.append(StringPool.QUESTION);
2178 sm.append(queryString);
2179
2180 return sm.toString();
2181 }
2182
2183 private String _getPathContext() {
2184 return _pathContext;
2185 }
2186
2187 private String _getPathFriendlyURLPrivateGroup() {
2188 return _pathFriendlyURLPrivateGroup;
2189 }
2190
2191 private String _getPathFriendlyURLPrivateUser() {
2192 return _pathFriendlyURLPrivateUser;
2193 }
2194
2195 private String _getPathFriendlyURLPublic() {
2196 return _pathFriendlyURLPublic;
2197 }
2198
2199 private String _getPathImage() {
2200 return _pathImage;
2201 }
2202
2203 private String _getPathMain() {
2204 return _pathMain;
2205 }
2206
2207 private String[] _getSystemCommunityRoles() {
2208 return _allSystemCommunityRoles;
2209 }
2210
2211 private String[] _getSystemGroups() {
2212 return _allSystemGroups;
2213 }
2214
2215 private String[] _getSystemRoles() {
2216 return _allSystemRoles;
2217 }
2218
2219 private boolean _isSystemGroup(String groupName) {
2220 if (groupName == null) {
2221 return false;
2222 }
2223
2224 groupName = groupName.trim();
2225
2226 int pos = Arrays.binarySearch(
2227 _sortedSystemGroups, groupName, new StringComparator());
2228
2229 if (pos >= 0) {
2230 return true;
2231 }
2232 else {
2233 return false;
2234 }
2235 }
2236
2237 private boolean _isSystemRole(String roleName) {
2238 if (roleName == null) {
2239 return false;
2240 }
2241
2242 roleName = roleName.trim();
2243
2244 int pos = Arrays.binarySearch(
2245 _sortedSystemRoles, roleName, new StringComparator());
2246
2247 if (pos >= 0) {
2248 return true;
2249 }
2250 else {
2251 pos = Arrays.binarySearch(
2252 _sortedSystemCommunityRoles, roleName, new StringComparator());
2253
2254 if (pos >= 0) {
2255 return true;
2256 }
2257 }
2258
2259 return false;
2260 }
2261
2262 private static Log _log = LogFactory.getLog(PortalUtil.class);
2263
2264 private static PortalUtil _instance = new PortalUtil();
2265
2266 private String _portalLibDir;
2267 private String _cdnHost;
2268 private String _pathContext;
2269 private String _pathFriendlyURLPrivateGroup;
2270 private String _pathFriendlyURLPrivateUser;
2271 private String _pathFriendlyURLPublic;
2272 private String _pathImage;
2273 private String _pathMain;
2274 private String[] _allSystemCommunityRoles;
2275 private String[] _allSystemGroups;
2276 private String[] _allSystemRoles;
2277 private String[] _sortedSystemCommunityRoles;
2278 private String[] _sortedSystemGroups;
2279 private String[] _sortedSystemRoles;
2280 private Set _reservedParams;
2281
2282}