1
14
15 package com.liferay.portal.util;
16
17 import com.liferay.portal.NoSuchCompanyException;
18 import com.liferay.portal.NoSuchLayoutException;
19 import com.liferay.portal.NoSuchUserException;
20 import com.liferay.portal.PortalException;
21 import com.liferay.portal.SystemException;
22 import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
23 import com.liferay.portal.kernel.configuration.Filter;
24 import com.liferay.portal.kernel.dao.db.DB;
25 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
26 import com.liferay.portal.kernel.language.LanguageUtil;
27 import com.liferay.portal.kernel.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
30 import com.liferay.portal.kernel.portlet.FriendlyURLMapperThreadLocal;
31 import com.liferay.portal.kernel.portlet.LiferayPortletMode;
32 import com.liferay.portal.kernel.portlet.PortletBag;
33 import com.liferay.portal.kernel.portlet.PortletBagPool;
34 import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
35 import com.liferay.portal.kernel.servlet.HttpHeaders;
36 import com.liferay.portal.kernel.servlet.HttpMethods;
37 import com.liferay.portal.kernel.servlet.ServletContextUtil;
38 import com.liferay.portal.kernel.servlet.SessionErrors;
39 import com.liferay.portal.kernel.servlet.StringServletResponse;
40 import com.liferay.portal.kernel.servlet.WebDirDetector;
41 import com.liferay.portal.kernel.upload.UploadPortletRequest;
42 import com.liferay.portal.kernel.upload.UploadServletRequest;
43 import com.liferay.portal.kernel.util.ArrayUtil;
44 import com.liferay.portal.kernel.util.Base64;
45 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
46 import com.liferay.portal.kernel.util.CharPool;
47 import com.liferay.portal.kernel.util.ClassUtil;
48 import com.liferay.portal.kernel.util.ContentTypes;
49 import com.liferay.portal.kernel.util.GetterUtil;
50 import com.liferay.portal.kernel.util.Http;
51 import com.liferay.portal.kernel.util.HttpUtil;
52 import com.liferay.portal.kernel.util.InstancePool;
53 import com.liferay.portal.kernel.util.JavaConstants;
54 import com.liferay.portal.kernel.util.LocaleUtil;
55 import com.liferay.portal.kernel.util.ParamUtil;
56 import com.liferay.portal.kernel.util.PropsKeys;
57 import com.liferay.portal.kernel.util.ReleaseInfo;
58 import com.liferay.portal.kernel.util.StringBundler;
59 import com.liferay.portal.kernel.util.StringComparator;
60 import com.liferay.portal.kernel.util.StringPool;
61 import com.liferay.portal.kernel.util.StringUtil;
62 import com.liferay.portal.kernel.util.UnicodeProperties;
63 import com.liferay.portal.kernel.util.Validator;
64 import com.liferay.portal.model.BaseModel;
65 import com.liferay.portal.model.ClassName;
66 import com.liferay.portal.model.ColorScheme;
67 import com.liferay.portal.model.Company;
68 import com.liferay.portal.model.CompanyConstants;
69 import com.liferay.portal.model.Group;
70 import com.liferay.portal.model.GroupConstants;
71 import com.liferay.portal.model.Layout;
72 import com.liferay.portal.model.LayoutConstants;
73 import com.liferay.portal.model.LayoutSet;
74 import com.liferay.portal.model.LayoutTypePortlet;
75 import com.liferay.portal.model.Organization;
76 import com.liferay.portal.model.Portlet;
77 import com.liferay.portal.model.PortletApp;
78 import com.liferay.portal.model.PublicRenderParameter;
79 import com.liferay.portal.model.Resource;
80 import com.liferay.portal.model.ResourceCode;
81 import com.liferay.portal.model.ResourcePermission;
82 import com.liferay.portal.model.Role;
83 import com.liferay.portal.model.RoleConstants;
84 import com.liferay.portal.model.Theme;
85 import com.liferay.portal.model.User;
86 import com.liferay.portal.model.UserGroup;
87 import com.liferay.portal.plugin.PluginPackageUtil;
88 import com.liferay.portal.security.auth.PrincipalException;
89 import com.liferay.portal.security.permission.ActionKeys;
90 import com.liferay.portal.security.permission.PermissionChecker;
91 import com.liferay.portal.security.permission.PermissionCheckerFactory;
92 import com.liferay.portal.service.ClassNameLocalServiceUtil;
93 import com.liferay.portal.service.ClassNameServiceUtil;
94 import com.liferay.portal.service.CompanyLocalServiceUtil;
95 import com.liferay.portal.service.GroupLocalServiceUtil;
96 import com.liferay.portal.service.LayoutLocalServiceUtil;
97 import com.liferay.portal.service.PortletLocalServiceUtil;
98 import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
99 import com.liferay.portal.service.UserLocalServiceUtil;
100 import com.liferay.portal.service.UserServiceUtil;
101 import com.liferay.portal.service.permission.UserPermissionUtil;
102 import com.liferay.portal.servlet.filters.i18n.I18nFilter;
103 import com.liferay.portal.struts.StrutsUtil;
104 import com.liferay.portal.theme.ThemeDisplay;
105 import com.liferay.portal.upload.UploadPortletRequestImpl;
106 import com.liferay.portal.upload.UploadServletRequestImpl;
107 import com.liferay.portlet.ActionResponseImpl;
108 import com.liferay.portlet.PortletConfigFactory;
109 import com.liferay.portlet.PortletConfigImpl;
110 import com.liferay.portlet.PortletContextImpl;
111 import com.liferay.portlet.PortletInstanceFactory;
112 import com.liferay.portlet.PortletPreferencesImpl;
113 import com.liferay.portlet.PortletPreferencesWrapper;
114 import com.liferay.portlet.PortletRequestImpl;
115 import com.liferay.portlet.PortletResponseImpl;
116 import com.liferay.portlet.PortletURLImpl;
117 import com.liferay.portlet.RenderRequestImpl;
118 import com.liferay.portlet.RenderResponseImpl;
119 import com.liferay.portlet.UserAttributes;
120 import com.liferay.portlet.blogs.model.BlogsEntry;
121 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
122 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
123 import com.liferay.portlet.imagegallery.model.IGImage;
124 import com.liferay.portlet.messageboards.model.MBMessage;
125 import com.liferay.portlet.social.util.FacebookUtil;
126 import com.liferay.portlet.wiki.model.WikiPage;
127 import com.liferay.util.Encryptor;
128 import com.liferay.util.JS;
129 import com.liferay.util.UniqueList;
130 import com.liferay.util.servlet.DynamicServletRequest;
131
132 import java.io.File;
133 import java.io.IOException;
134
135 import java.lang.reflect.Method;
136
137 import java.net.InetAddress;
138 import java.net.UnknownHostException;
139
140 import java.rmi.RemoteException;
141
142 import java.util.Arrays;
143 import java.util.Calendar;
144 import java.util.Date;
145 import java.util.Enumeration;
146 import java.util.HashMap;
147 import java.util.HashSet;
148 import java.util.Iterator;
149 import java.util.List;
150 import java.util.Locale;
151 import java.util.Map;
152 import java.util.Properties;
153 import java.util.ResourceBundle;
154 import java.util.Set;
155 import java.util.TimeZone;
156 import java.util.concurrent.ConcurrentHashMap;
157
158 import javax.portlet.ActionRequest;
159 import javax.portlet.ActionResponse;
160 import javax.portlet.PortletConfig;
161 import javax.portlet.PortletMode;
162 import javax.portlet.PortletPreferences;
163 import javax.portlet.PortletRequest;
164 import javax.portlet.PortletResponse;
165 import javax.portlet.PortletURL;
166 import javax.portlet.PreferencesValidator;
167 import javax.portlet.RenderRequest;
168 import javax.portlet.ValidatorException;
169 import javax.portlet.WindowState;
170 import javax.portlet.filter.PortletRequestWrapper;
171 import javax.portlet.filter.PortletResponseWrapper;
172
173 import javax.servlet.RequestDispatcher;
174 import javax.servlet.ServletContext;
175 import javax.servlet.ServletException;
176 import javax.servlet.http.HttpServletRequest;
177 import javax.servlet.http.HttpServletRequestWrapper;
178 import javax.servlet.http.HttpServletResponse;
179 import javax.servlet.http.HttpSession;
180
181 import javax.xml.namespace.QName;
182
183 import org.apache.struts.Globals;
184
185
195 public class PortalImpl implements Portal {
196
197 public PortalImpl() {
198
199
201 _computerName = System.getProperty("env.COMPUTERNAME");
202
203 if (Validator.isNull(_computerName)) {
204 _computerName = System.getProperty("env.HOST");
205 }
206
207 if (Validator.isNull(_computerName)) {
208 _computerName = System.getProperty("env.HOSTNAME");
209 }
210
211 if (Validator.isNull(_computerName)) {
212 try {
213 _computerName = InetAddress.getLocalHost().getHostName();
214 }
215 catch (UnknownHostException uhe) {
216 }
217 }
218
219 try {
220 _computerAddress = InetAddress.getByName(
221 _computerName).getHostAddress();
222 }
223 catch (UnknownHostException uhe) {
224 }
225
226 if (Validator.isNull(_computerAddress)) {
227 try {
228 _computerAddress = InetAddress.getLocalHost().getHostAddress();
229 }
230 catch (UnknownHostException uhe) {
231 }
232 }
233
234
236 _globalLibDir = ClassUtil.getParentPath(
237 ReleaseInfo.class.getClassLoader(), ReleaseInfo.class.getName());
238
239 int pos = _globalLibDir.lastIndexOf(".jar!");
240
241 pos = _globalLibDir.lastIndexOf(StringPool.SLASH, pos);
242
243 _globalLibDir = _globalLibDir.substring(0, pos + 1);
244
245 if (_log.isInfoEnabled()) {
246 _log.info("Global lib directory " + _globalLibDir);
247 }
248
249
251 ClassLoader classLoader = getClass().getClassLoader();
252
253 _portalLibDir = WebDirDetector.getLibDir(classLoader);
254
255 String portalLibDir = System.getProperty("liferay.lib.portal.dir");
256
257 if (portalLibDir != null) {
258 if (!portalLibDir.endsWith(StringPool.SLASH)) {
259 portalLibDir += StringPool.SLASH;
260 }
261
262 _portalLibDir = portalLibDir;
263 }
264
265 if (_log.isInfoEnabled()) {
266 _log.info("Portal lib directory " + _portalLibDir);
267 }
268
269 _portalWebDir = WebDirDetector.getRootDir(_portalLibDir);
270
271 if (_log.isDebugEnabled()) {
272 _log.debug("Portal web directory " + _portalWebDir);
273 }
274
275
277 _cdnHostHttp = PropsValues.CDN_HOST_HTTP;
278
279 if (_cdnHostHttp.startsWith("${")) {
280 _cdnHostHttp = StringPool.BLANK;
281 }
282
283 _cdnHostHttps = PropsValues.CDN_HOST_HTTPS;
284
285 if (_cdnHostHttps.startsWith("${")) {
286 _cdnHostHttps = StringPool.BLANK;
287 }
288
289
291 _pathContext = PropsUtil.get(PropsKeys.PORTAL_CTX);
292
293 if (_pathContext.equals(StringPool.SLASH)) {
294 _pathContext = StringPool.BLANK;
295 }
296
297 _pathFriendlyURLPrivateGroup =
298 _pathContext + _PRIVATE_GROUP_SERVLET_MAPPING;
299 _pathFriendlyURLPrivateUser =
300 _pathContext + _PRIVATE_USER_SERVLET_MAPPING;
301 _pathFriendlyURLPublic = _pathContext + _PUBLIC_GROUP_SERVLET_MAPPING;
302 _pathImage = _pathContext + PATH_IMAGE;
303 _pathMain = _pathContext + PATH_MAIN;
304
305
307 String customSystemGroups[] =
308 PropsUtil.getArray(PropsKeys.SYSTEM_GROUPS);
309
310 if ((customSystemGroups == null) || (customSystemGroups.length == 0)) {
311 _allSystemGroups = GroupConstants.SYSTEM_GROUPS;
312 }
313 else {
314 _allSystemGroups = ArrayUtil.append(
315 GroupConstants.SYSTEM_GROUPS, customSystemGroups);
316 }
317
318 _sortedSystemGroups = new String[_allSystemGroups.length];
319
320 System.arraycopy(
321 _allSystemGroups, 0, _sortedSystemGroups, 0,
322 _allSystemGroups.length);
323
324 Arrays.sort(_sortedSystemGroups, new StringComparator());
325
326
328 String customSystemRoles[] = PropsUtil.getArray(PropsKeys.SYSTEM_ROLES);
329
330 if ((customSystemRoles == null) || (customSystemRoles.length == 0)) {
331 _allSystemRoles = RoleConstants.SYSTEM_ROLES;
332 }
333 else {
334 _allSystemRoles = ArrayUtil.append(
335 RoleConstants.SYSTEM_ROLES, customSystemRoles);
336 }
337
338 _sortedSystemRoles = new String[_allSystemRoles.length];
339
340 System.arraycopy(
341 _allSystemRoles, 0, _sortedSystemRoles, 0, _allSystemRoles.length);
342
343 Arrays.sort(_sortedSystemRoles, new StringComparator());
344
345
347 String customSystemCommunityRoles[] =
348 PropsUtil.getArray(PropsKeys.SYSTEM_COMMUNITY_ROLES);
349
350 if ((customSystemCommunityRoles == null) ||
351 (customSystemCommunityRoles.length == 0)) {
352
353 _allSystemCommunityRoles = RoleConstants.SYSTEM_COMMUNITY_ROLES;
354 }
355 else {
356 _allSystemCommunityRoles = ArrayUtil.append(
357 RoleConstants.SYSTEM_COMMUNITY_ROLES,
358 customSystemCommunityRoles);
359 }
360
361 _sortedSystemCommunityRoles =
362 new String[_allSystemCommunityRoles.length];
363
364 System.arraycopy(
365 _allSystemCommunityRoles, 0, _sortedSystemCommunityRoles, 0,
366 _allSystemCommunityRoles.length);
367
368 Arrays.sort(_sortedSystemCommunityRoles, new StringComparator());
369
370
372 String customSystemOrganizationRoles[] =
373 PropsUtil.getArray(PropsKeys.SYSTEM_ORGANIZATION_ROLES);
374
375 if ((customSystemOrganizationRoles == null) ||
376 (customSystemOrganizationRoles.length == 0)) {
377
378 _allSystemOrganizationRoles =
379 RoleConstants.SYSTEM_ORGANIZATION_ROLES;
380 }
381 else {
382 _allSystemOrganizationRoles = ArrayUtil.append(
383 RoleConstants.SYSTEM_ORGANIZATION_ROLES,
384 customSystemOrganizationRoles);
385 }
386
387 _sortedSystemOrganizationRoles =
388 new String[_allSystemOrganizationRoles.length];
389
390 System.arraycopy(
391 _allSystemOrganizationRoles, 0, _sortedSystemOrganizationRoles, 0,
392 _allSystemOrganizationRoles.length);
393
394 Arrays.sort(_sortedSystemOrganizationRoles, new StringComparator());
395
396
398 _reservedParams = new HashSet<String>();
399
400 _reservedParams.add("p_l_id");
401 _reservedParams.add("p_l_reset");
402 _reservedParams.add("p_p_id");
403 _reservedParams.add("p_p_lifecycle");
404 _reservedParams.add("p_p_url_type");
405 _reservedParams.add("p_p_state");
406 _reservedParams.add("p_p_mode");
407 _reservedParams.add("p_p_resource_id");
408 _reservedParams.add("p_p_cacheability");
409 _reservedParams.add("p_p_width");
410 _reservedParams.add("p_p_col_id");
411 _reservedParams.add("p_p_col_pos");
412 _reservedParams.add("p_p_col_count");
413 _reservedParams.add("p_p_static");
414 _reservedParams.add("saveLastPath");
415 }
416
417
421 public void addPageDescription(
422 String description, HttpServletRequest request) {
423
424 String requestDescription = (String)request.getAttribute(
425 WebKeys.PAGE_DESCRIPTION);
426
427 if (requestDescription != null) {
428 description = requestDescription + StringPool.SPACE + description;
429 }
430
431 request.setAttribute(WebKeys.PAGE_DESCRIPTION, description);
432 }
433
434
437 public void addPageKeywords(String keywords, HttpServletRequest request) {
438 List<String> requestKeywords = (List<String>)request.getAttribute(
439 WebKeys.PAGE_KEYWORDS);
440
441 if (requestKeywords == null) {
442 requestKeywords = new UniqueList<String>();
443 }
444
445 String[] keywordsArray = StringUtil.split(keywords);
446
447 for (String keyword : keywordsArray) {
448 if (!requestKeywords.contains(keyword.toLowerCase())) {
449 requestKeywords.add(keyword.toLowerCase());
450 }
451 }
452
453 request.setAttribute(WebKeys.PAGE_KEYWORDS, requestKeywords);
454 }
455
456
459 public void addPageSubtitle(String subtitle, HttpServletRequest request) {
460 String requestSubtitle = (String)request.getAttribute(
461 WebKeys.PAGE_SUBTITLE);
462
463 if (requestSubtitle != null) {
464 subtitle = requestSubtitle + StringPool.SPACE + subtitle;
465 }
466
467 request.setAttribute(WebKeys.PAGE_SUBTITLE, subtitle);
468 }
469
470
474 public void addPageTitle(String title, HttpServletRequest request) {
475 String requestTitle = (String)request.getAttribute(WebKeys.PAGE_TITLE);
476
477 if (requestTitle != null) {
478 title = requestTitle + StringPool.SPACE + title;
479 }
480
481 request.setAttribute(WebKeys.PAGE_TITLE, title);
482 }
483
484 public void clearRequestParameters(RenderRequest renderRequest) {
485
486
488 ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
489 WebKeys.THEME_DISPLAY);
490
491 if (themeDisplay.isLifecycleAction()) {
492 ((RenderRequestImpl)renderRequest).getRenderParameters().clear();
493 }
494 }
495
496 public void copyRequestParameters(
497 ActionRequest actionRequest, ActionResponse actionResponse) {
498
499 try {
500 ActionResponseImpl actionResponseImpl =
501 (ActionResponseImpl)actionResponse;
502
503 Map<String, String[]> renderParameters =
504 actionResponseImpl.getRenderParameterMap();
505
506 actionResponse.setRenderParameter("p_p_lifecycle", "1");
507
508 Enumeration<String> enu = actionRequest.getParameterNames();
509
510 while (enu.hasMoreElements()) {
511 String param = enu.nextElement();
512 String[] values = actionRequest.getParameterValues(param);
513
514 if (renderParameters.get(
515 actionResponseImpl.getNamespace() + param) == null) {
516
517 actionResponse.setRenderParameter(param, values);
518 }
519 }
520 }
521 catch (IllegalStateException ise) {
522
523
526 }
527 }
528
529
532 public void destroyPortletInstance(Portlet portlet) {
533 PortletInstanceFactory.destroy(portlet);
534 }
535
536 public String escapeRedirect(String url) {
537 if (Validator.isNull(url) || !HttpUtil.hasDomain(url)) {
538 return url;
539 }
540
541 try {
542 String securityMode = PropsValues.REDIRECT_URL_SECURITY_MODE;
543
544 String domain = StringUtil.split(
545 HttpUtil.getDomain(url), StringPool.COLON)[0];
546
547 if (securityMode.equals("domain")) {
548 String[] allowedDomains =
549 PropsValues.REDIRECT_URL_DOMAINS_ALLOWED;
550
551 if ((allowedDomains.length > 0) &&
552 !ArrayUtil.contains(allowedDomains, domain)) {
553
554 url = null;
555 }
556 }
557 else if (securityMode.equals("ip")) {
558 String[] allowedIps = PropsValues.REDIRECT_URL_IPS_ALLOWED;
559
560 InetAddress inetAddress = InetAddress.getByName(domain);
561
562 if ((allowedIps.length > 0) &&
563 !ArrayUtil.contains(
564 allowedIps, inetAddress.getHostAddress())) {
565
566 String serverIp = getComputerAddress();
567
568 if (!serverIp.equals(inetAddress.getHostAddress()) ||
569 !ArrayUtil.contains(allowedIps, "SERVER_IP")) {
570
571 url = null;
572 }
573 }
574 }
575 }
576 catch (UnknownHostException uhe) {
577 url = null;
578 }
579
580 return url;
581 }
582
583 public BaseModel<?> getBaseModel(Resource resource)
584 throws PortalException, SystemException {
585
586 ResourceCode resourceCode =
587 ResourceCodeLocalServiceUtil.getResourceCode(resource.getCodeId());
588
589 String modelName = resourceCode.getName();
590 String primKey = resource.getPrimKey();
591
592 return getBaseModel(modelName, primKey);
593 }
594
595 public BaseModel<?> getBaseModel(ResourcePermission resourcePermission)
596 throws PortalException, SystemException {
597
598 String modelName = resourcePermission.getName();
599 String primKey = resourcePermission.getPrimKey();
600
601 return getBaseModel(modelName, primKey);
602 }
603
604 public BaseModel<?> getBaseModel(String modelName, String primKey)
605 throws PortalException, SystemException {
606
607 if (!modelName.contains(".model.")) {
608 return null;
609 }
610
611 String[] parts = StringUtil.split(modelName, StringPool.PERIOD);
612
613 if ((parts.length <= 2) || !parts[parts.length - 2].equals("model")) {
614 return null;
615 }
616
617 parts[parts.length - 2] = "service";
618
619 String serviceName =
620 StringUtil.merge(parts, StringPool.PERIOD) + "LocalServiceUtil";
621 String methodName = "get" + parts[parts.length - 1];
622
623 Method method = null;
624
625 try {
626 Class<?> serviceUtil = Class.forName(serviceName);
627
628 if (Validator.isNumber(primKey)) {
629 method = serviceUtil.getMethod(
630 methodName, new Class[] {Long.TYPE});
631
632 return (BaseModel<?>)method.invoke(null, new Long(primKey));
633 }
634 else {
635 method = serviceUtil.getMethod(
636 methodName, new Class[] {String.class});
637
638 return (BaseModel<?>)method.invoke(null, primKey);
639 }
640 }
641 catch (Exception e) {
642 Throwable cause = e.getCause();
643
644 if (cause instanceof PortalException) {
645 throw (PortalException)cause;
646 }
647 else if (cause instanceof SystemException) {
648 throw (SystemException)cause;
649 }
650 else {
651 throw new SystemException(cause);
652 }
653 }
654 }
655
656 public long getBasicAuthUserId(HttpServletRequest request)
657 throws PortalException, SystemException {
658
659 long companyId = PortalInstances.getCompanyId(request);
660
661 return getBasicAuthUserId(request, companyId);
662 }
663
664 public long getBasicAuthUserId(HttpServletRequest request, long companyId)
665 throws PortalException, SystemException {
666
667 long userId = 0;
668
669 String authorizationHeader = request.getHeader(
670 HttpHeaders.AUTHORIZATION);
671
672 if (Validator.isNull(authorizationHeader)) {
673 return userId;
674 }
675
676 String[] authorizationArray = authorizationHeader.split("\\s+");
677
678 String authorization = authorizationArray[0];
679 String credentials = new String(Base64.decode(authorizationArray[1]));
680
681 if (!authorization.equalsIgnoreCase(HttpServletRequest.BASIC_AUTH)) {
682 return userId;
683 }
684
685 String[] loginAndPassword = StringUtil.split(
686 credentials, StringPool.COLON);
687
688 String login = loginAndPassword[0].trim();
689
690 String password = null;
691
692 if (loginAndPassword.length > 1) {
693 password = loginAndPassword[1].trim();
694 }
695
696
698 if (login.endsWith("@uid")) {
699 int pos = login.indexOf("@uid");
700
701 login = login.substring(0, pos);
702 }
703 else if (login.endsWith("@sn")) {
704 int pos = login.indexOf("@sn");
705
706 login = login.substring(0, pos);
707 }
708
709
711 userId = UserLocalServiceUtil.authenticateForBasic(
712 companyId, CompanyConstants.AUTH_TYPE_EA, login, password);
713
714 if (userId > 0) {
715 return userId;
716 }
717
718 userId = UserLocalServiceUtil.authenticateForBasic(
719 companyId, CompanyConstants.AUTH_TYPE_SN, login, password);
720
721 if (userId > 0) {
722 return userId;
723 }
724
725 userId = UserLocalServiceUtil.authenticateForBasic(
726 companyId, CompanyConstants.AUTH_TYPE_ID, login, password);
727
728 return userId;
729 }
730
731
734 public String getCDNHost() {
735 return getCDNHostHttp();
736 }
737
738 public String getCDNHost(boolean secure) {
739 if (secure) {
740 return getCDNHostHttps();
741 }
742 else {
743 return getCDNHostHttp();
744 }
745 }
746
747 public String getCDNHostHttp() {
748 return _cdnHostHttp;
749 }
750
751 public String getCDNHostHttps() {
752 return _cdnHostHttps;
753 }
754
755 public String getClassName(long classNameId) {
756 try {
757 ClassName className = ClassNameServiceUtil.getClassName(
758 classNameId);
759
760 return className.getValue();
761 }
762 catch (Exception e) {
763 throw new RuntimeException(
764 "Unable to get class name from id " + classNameId);
765 }
766 }
767
768 public long getClassNameId(Class<?> classObj) {
769 return ClassNameLocalServiceUtil.getClassNameId(classObj);
770 }
771
772 public long getClassNameId(String value) {
773 return ClassNameLocalServiceUtil.getClassNameId(value);
774 }
775
776 public String getClassNamePortletId(String className) {
777 String portletId = StringPool.BLANK;
778
779 if (className.startsWith("com.liferay.portlet.blogs")) {
780 portletId = PortletKeys.BLOGS;
781 }
782 else if (className.startsWith("com.liferay.portlet.bookmarks")) {
783 portletId = PortletKeys.BOOKMARKS;
784 }
785 else if (className.startsWith("com.liferay.portlet.documentlibrary")) {
786 portletId = PortletKeys.DOCUMENT_LIBRARY;
787 }
788 else if (className.startsWith("com.liferay.portlet.imagegallery")) {
789 portletId = PortletKeys.IMAGE_GALLERY;
790 }
791 else if (className.startsWith("com.liferay.portlet.journal")) {
792 portletId = PortletKeys.JOURNAL;
793 }
794 else if (className.startsWith("com.liferay.portlet.messageboards")) {
795 portletId = PortletKeys.MESSAGE_BOARDS;
796 }
797 else if (className.startsWith("com.liferay.portlet.wiki")) {
798 portletId = PortletKeys.WIKI;
799 }
800
801 return portletId;
802 }
803
804 public String getCommunityLoginURL(ThemeDisplay themeDisplay)
805 throws PortalException, SystemException {
806
807 if (Validator.isNull(PropsValues.AUTH_LOGIN_COMMUNITY_URL)) {
808 return null;
809 }
810
811 for (Layout layout : themeDisplay.getLayouts()) {
812 if (layout.getFriendlyURL().equals(
813 PropsValues.AUTH_LOGIN_COMMUNITY_URL)) {
814
815 if (themeDisplay.getLayout() != null) {
816 String layoutSetFriendlyURL = getLayoutSetFriendlyURL(
817 themeDisplay.getLayout().getLayoutSet(), themeDisplay);
818
819 return layoutSetFriendlyURL +
820 PropsValues.AUTH_LOGIN_COMMUNITY_URL;
821 }
822
823 break;
824 }
825 }
826
827 return null;
828 }
829
830 public String[] getCommunityPermissions(HttpServletRequest request) {
831 return request.getParameterValues("communityPermissions");
832 }
833
834 public String[] getCommunityPermissions(PortletRequest portletRequest) {
835 return portletRequest.getParameterValues("communityPermissions");
836 }
837
838 public Company getCompany(HttpServletRequest request)
839 throws PortalException, SystemException {
840
841 long companyId = getCompanyId(request);
842
843 if (companyId <= 0) {
844 return null;
845 }
846
847 Company company = (Company)request.getAttribute(WebKeys.COMPANY);
848
849 if (company == null) {
850
851
853 try {
854 company = CompanyLocalServiceUtil.getCompanyById(companyId);
855 }
856 catch (NoSuchCompanyException nsce) {
857 company = CompanyLocalServiceUtil.getCompanyById(
858 PortalInstances.getDefaultCompanyId());
859 }
860
861 request.setAttribute(WebKeys.COMPANY, company);
862 }
863
864 return company;
865 }
866
867 public Company getCompany(PortletRequest portletRequest)
868 throws PortalException, SystemException {
869
870 return getCompany(getHttpServletRequest(portletRequest));
871 }
872
873 public long getCompanyId(HttpServletRequest request) {
874 return PortalInstances.getCompanyId(request);
875 }
876
877 public long getCompanyId(PortletRequest portletRequest) {
878 return getCompanyId(getHttpServletRequest(portletRequest));
879 }
880
881 public long getCompanyIdByWebId(ServletContext servletContext) {
882 String webId = GetterUtil.getString(
883 servletContext.getInitParameter("company_web_id"));
884
885 return getCompanyIdByWebId(webId);
886 }
887
888 public long getCompanyIdByWebId(String webId) {
889 long companyId = 0;
890
891 try {
892 Company company = CompanyLocalServiceUtil.getCompanyByWebId(webId);
893
894 companyId = company.getCompanyId();
895 }
896 catch (Exception e) {
897 _log.error(e.getMessage());
898 }
899
900 return companyId;
901 }
902
903 public long[] getCompanyIds() {
904 return PortalInstances.getCompanyIds();
905 }
906
907 public String getComputerAddress() {
908 return _computerAddress;
909 }
910
911 public String getComputerName() {
912 return _computerName;
913 }
914
915 public String getCurrentCompleteURL(HttpServletRequest request) {
916 String currentCompleteURL = (String)request.getAttribute(
917 WebKeys.CURRENT_COMPLETE_URL);
918
919 if (currentCompleteURL == null) {
920 currentCompleteURL = HttpUtil.getCompleteURL(request);
921
922 request.setAttribute(
923 WebKeys.CURRENT_COMPLETE_URL, currentCompleteURL);
924 }
925
926 return currentCompleteURL;
927 }
928
929 public String getCurrentURL(HttpServletRequest request) {
930 String currentURL = (String)request.getAttribute(WebKeys.CURRENT_URL);
931
932 if (currentURL == null) {
933 currentURL = ParamUtil.getString(request, "currentURL");
934
935 if (Validator.isNull(currentURL)) {
936 currentURL = HttpUtil.getCompleteURL(request);
937
938 if ((Validator.isNotNull(currentURL)) &&
939 (currentURL.indexOf(_J_SECURITY_CHECK) == -1)) {
940
941 currentURL = currentURL.substring(
942 currentURL.indexOf(Http.PROTOCOL_DELIMITER) +
943 Http.PROTOCOL_DELIMITER.length());
944
945 currentURL = currentURL.substring(
946 currentURL.indexOf(StringPool.SLASH));
947 }
948
949 if (Validator.isNotNull(currentURL) &&
950 FacebookUtil.isFacebook(currentURL)) {
951
952 String[] facebookData = FacebookUtil.getFacebookData(
953 request);
954
955 currentURL =
956 FacebookUtil.FACEBOOK_APPS_URL + facebookData[0] +
957 facebookData[2];
958 }
959 }
960
961 if (Validator.isNull(currentURL)) {
962 currentURL = getPathMain();
963 }
964
965 request.setAttribute(WebKeys.CURRENT_URL, currentURL);
966 }
967
968 return currentURL;
969 }
970
971 public String getCurrentURL(PortletRequest portletRequest) {
972 return (String)portletRequest.getAttribute(WebKeys.CURRENT_URL);
973 }
974
975 public String getCustomSQLFunctionIsNotNull() {
976 return PropsValues.CUSTOM_SQL_FUNCTION_ISNOTNULL;
977 }
978
979 public String getCustomSQLFunctionIsNull() {
980 return PropsValues.CUSTOM_SQL_FUNCTION_ISNULL;
981 }
982
983 public Date getDate(
984 int month, int day, int year, int hour, int min, PortalException pe)
985 throws PortalException {
986
987 return getDate(month, day, year, hour, min, null, pe);
988 }
989
990 public Date getDate(
991 int month, int day, int year, int hour, int min, TimeZone timeZone,
992 PortalException pe)
993 throws PortalException {
994
995 if (!Validator.isGregorianDate(month, day, year)) {
996 throw pe;
997 }
998 else {
999 Calendar cal = null;
1000
1001 if (timeZone == null) {
1002 cal = CalendarFactoryUtil.getCalendar();
1003 }
1004 else {
1005 cal = CalendarFactoryUtil.getCalendar(timeZone);
1006 }
1007
1008 if ((hour == -1) || (min == -1)) {
1009 cal.set(year, month, day, 0, 0, 0);
1010 }
1011 else {
1012 cal.set(year, month, day, hour, min, 0);
1013 }
1014
1015 cal.set(Calendar.MILLISECOND, 0);
1016
1017 Date date = cal.getTime();
1018
1019
1024
1025 return date;
1026 }
1027 }
1028
1029 public Date getDate(int month, int day, int year, PortalException pe)
1030 throws PortalException {
1031
1032 return getDate(month, day, year, null, pe);
1033 }
1034
1035 public Date getDate(
1036 int month, int day, int year, TimeZone timeZone, PortalException pe)
1037 throws PortalException {
1038
1039 return getDate(month, day, year, -1, -1, timeZone, pe);
1040 }
1041
1042 public long getDefaultCompanyId() {
1043 return PortalInstances.getDefaultCompanyId();
1044 }
1045
1046 public String getGlobalLibDir() {
1047 return _globalLibDir;
1048 }
1049
1050 public String getGoogleGadgetURL(
1051 Portlet portlet, ThemeDisplay themeDisplay) {
1052
1053 return _getServletURL(
1054 portlet, PropsValues.GOOGLE_GADGET_SERVLET_MAPPING, themeDisplay);
1055 }
1056
1057 public String[] getGuestPermissions(HttpServletRequest request) {
1058 String[] guestPermissions = request.getParameterValues(
1059 "guestPermissions");
1060
1061 if (guestPermissions == null) {
1062 return null;
1063 }
1064
1065 boolean inputPermissionsPublic = ParamUtil.getBoolean(
1066 request, "inputPermissionsPublic");
1067 boolean inputPermissionsShowConfigure = ParamUtil.getBoolean(
1068 request, "inputPermissionsShowConfigure");
1069
1070 if (!inputPermissionsShowConfigure) {
1071 if (!inputPermissionsPublic) {
1072 guestPermissions = new String[0];
1073 }
1074 else if ((guestPermissions == null) ||
1075 (guestPermissions.length == 0)) {
1076
1077 guestPermissions = new String[] {ActionKeys.VIEW};
1078 }
1079 }
1080
1081 return guestPermissions;
1082 }
1083
1084 public String[] getGuestPermissions(PortletRequest portletRequest) {
1085 String[] guestPermissions = portletRequest.getParameterValues(
1086 "guestPermissions");
1087
1088 if (guestPermissions == null) {
1089 return null;
1090 }
1091
1092 boolean inputPermissionsPublic = ParamUtil.getBoolean(
1093 portletRequest, "inputPermissionsPublic");
1094 boolean inputPermissionsShowConfigure = ParamUtil.getBoolean(
1095 portletRequest, "inputPermissionsShowConfigure");
1096
1097 if (!inputPermissionsShowConfigure) {
1098 if (!inputPermissionsPublic) {
1099 guestPermissions = new String[0];
1100 }
1101 else if ((guestPermissions == null) ||
1102 (guestPermissions.length == 0)) {
1103
1104 guestPermissions = new String[] {ActionKeys.VIEW};
1105 }
1106 }
1107
1108 return guestPermissions;
1109 }
1110
1111 public String getHost(HttpServletRequest request) {
1112 request = getOriginalServletRequest(request);
1113
1114 String host = request.getHeader("Host");
1115
1116 if (host != null) {
1117 host = host.trim().toLowerCase();
1118
1119 int pos = host.indexOf(':');
1120
1121 if (pos >= 0) {
1122 host = host.substring(0, pos);
1123 }
1124 }
1125 else {
1126 host = null;
1127 }
1128
1129 return host;
1130 }
1131
1132 public String getHost(PortletRequest portletRequest) {
1133 return getHost(getHttpServletRequest(portletRequest));
1134 }
1135
1136 public HttpServletRequest getHttpServletRequest(
1137 PortletRequest portletRequest) {
1138
1139 if (portletRequest instanceof PortletRequestImpl) {
1140 PortletRequestImpl portletRequestImpl =
1141 (PortletRequestImpl)portletRequest;
1142
1143 return portletRequestImpl.getHttpServletRequest();
1144 }
1145 else if (portletRequest instanceof PortletRequestWrapper) {
1146 PortletRequestWrapper portletRequestWrapper =
1147 (PortletRequestWrapper)portletRequest;
1148
1149 return getHttpServletRequest(portletRequestWrapper.getRequest());
1150 }
1151
1152 throw new RuntimeException(
1153 "Unable to get the HTTP servlet request from " +
1154 portletRequest.getClass().getName());
1155 }
1156
1157 public HttpServletResponse getHttpServletResponse(
1158 PortletResponse portletResponse) {
1159
1160 if (portletResponse instanceof ActionResponseImpl) {
1161 ActionResponseImpl actionResponseImpl =
1162 (ActionResponseImpl)portletResponse;
1163
1164 return actionResponseImpl.getHttpServletResponse();
1165 }
1166 else if (portletResponse instanceof RenderResponseImpl) {
1167 RenderResponseImpl renderResponseImpl =
1168 (RenderResponseImpl)portletResponse;
1169
1170 return renderResponseImpl.getHttpServletResponse();
1171 }
1172 else if (portletResponse instanceof PortletResponseWrapper) {
1173 PortletResponseWrapper portletResponseWrapper =
1174 (PortletResponseWrapper)portletResponse;
1175
1176 return getHttpServletResponse(portletResponseWrapper.getResponse());
1177 }
1178
1179 PortletResponseImpl portletResponseImpl =
1180 PortletResponseImpl.getPortletResponseImpl(portletResponse);
1181
1182 return portletResponseImpl.getHttpServletResponse();
1183 }
1184
1185 public String getJsSafePortletId(String portletId) {
1186 return JS.getSafeName(portletId);
1187 }
1188
1189 public String getLayoutActualURL(Layout layout) {
1190 return getLayoutActualURL(layout, getPathMain());
1191 }
1192
1193 public String getLayoutActualURL(Layout layout, String mainPath) {
1194 Map<String, String> variables = new HashMap<String, String>();
1195
1196 variables.put("liferay:groupId", String.valueOf(layout.getGroupId()));
1197 variables.put("liferay:mainPath", mainPath);
1198 variables.put("liferay:plid", String.valueOf(layout.getPlid()));
1199
1200 UnicodeProperties typeSettingsProperties =
1201 layout.getLayoutType().getTypeSettingsProperties();
1202
1203 Iterator<Map.Entry<String, String>> itr =
1204 typeSettingsProperties.entrySet().iterator();
1205
1206 while (itr.hasNext()) {
1207 Map.Entry<String, String> entry = itr.next();
1208
1209 String key = entry.getKey();
1210 String value = entry.getValue();
1211
1212 variables.put(key, value);
1213 }
1214
1215 String href = PropsUtil.get(
1216 PropsKeys.LAYOUT_URL, new Filter(layout.getType(), variables));
1217
1218 return href;
1219 }
1220
1221 public String getLayoutActualURL(
1222 long groupId, boolean privateLayout, String mainPath,
1223 String friendlyURL)
1224 throws PortalException, SystemException {
1225
1226 return getLayoutActualURL(
1227 groupId, privateLayout, mainPath, friendlyURL, null);
1228 }
1229
1230 public String getLayoutActualURL(
1231 long groupId, boolean privateLayout, String mainPath,
1232 String friendlyURL, Map<String, String[]> params)
1233 throws PortalException, SystemException {
1234
1235 Layout layout = null;
1236 String queryString = StringPool.BLANK;
1237
1238 if (Validator.isNull(friendlyURL)) {
1239 List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
1240 groupId, privateLayout,
1241 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1242
1243 if (layouts.size() > 0) {
1244 layout = layouts.get(0);
1245 }
1246 else {
1247 throw new NoSuchLayoutException(
1248 "{groupId=" + groupId + ",privateLayout=" + privateLayout +
1249 "} does not have any layouts");
1250 }
1251 }
1252 else {
1253 Object[] friendlyURLMapper = getPortletFriendlyURLMapper(
1254 groupId, privateLayout, friendlyURL, params);
1255
1256 layout = (Layout)friendlyURLMapper[0];
1257 queryString = (String)friendlyURLMapper[1];
1258 }
1259
1260 String layoutActualURL = getLayoutActualURL(layout, mainPath);
1261
1262 if (Validator.isNotNull(queryString)) {
1263 layoutActualURL = layoutActualURL + queryString;
1264 }
1265 else if (params.isEmpty()) {
1266 UnicodeProperties typeSettingsProperties =
1267 layout.getLayoutType().getTypeSettingsProperties();
1268
1269 queryString = typeSettingsProperties.getProperty("query-string");
1270
1271 if (Validator.isNotNull(queryString) &&
1272 layoutActualURL.contains(StringPool.QUESTION)) {
1273
1274 layoutActualURL =
1275 layoutActualURL + StringPool.AMPERSAND + queryString;
1276 }
1277 }
1278
1279 return layoutActualURL;
1280 }
1281
1282 public String getLayoutEditPage(Layout layout) {
1283 return PropsUtil.get(
1284 PropsKeys.LAYOUT_EDIT_PAGE, new Filter(layout.getType()));
1285 }
1286
1287 public String getLayoutFriendlyURL(
1288 Layout layout, ThemeDisplay themeDisplay) {
1289
1290 if (!isLayoutFriendliable(layout)) {
1291 return null;
1292 }
1293
1294 String layoutFriendlyURL = layout.getFriendlyURL();
1295
1296 LayoutSet layoutSet = layout.getLayoutSet();
1297
1298 long curLayoutSetId =
1299 themeDisplay.getLayout().getLayoutSet().getLayoutSetId();
1300
1301 String portalURL = StringPool.BLANK;
1302
1303 if (!themeDisplay.getServerName().equals(_LOCALHOST)) {
1304 String virtualHost = layoutSet.getVirtualHost();
1305
1306 if (Validator.isNull(virtualHost) &&
1307 Validator.isNotNull(
1308 PropsValues.VIRTUAL_HOSTS_DEFAULT_COMMUNITY_NAME)) {
1309
1310 try {
1311 Group group = GroupLocalServiceUtil.getGroup(
1312 themeDisplay.getCompanyId(),
1313 PropsValues.VIRTUAL_HOSTS_DEFAULT_COMMUNITY_NAME);
1314
1315 if (layoutSet.getGroupId() == group.getGroupId()) {
1316 Company company = themeDisplay.getCompany();
1317
1318 virtualHost = company.getVirtualHost();
1319 }
1320 }
1321 catch (Exception e) {
1322 _log.error(e, e);
1323 }
1324 }
1325
1326 if (Validator.isNotNull(virtualHost)) {
1327 virtualHost = getPortalURL(
1328 virtualHost, themeDisplay.getServerPort(),
1329 themeDisplay.isSecure());
1330
1331 String portalDomain = HttpUtil.getDomain(
1332 themeDisplay.getPortalURL());
1333
1334 if ((layoutSet.getLayoutSetId() != curLayoutSetId) ||
1335 (virtualHost.indexOf(portalDomain) != -1)) {
1336
1337 if (themeDisplay.isWidget()) {
1338 layoutFriendlyURL =
1339 PropsValues.WIDGET_SERVLET_MAPPING +
1340 layoutFriendlyURL;
1341 }
1342
1343 if (themeDisplay.isI18n()) {
1344 layoutFriendlyURL =
1345 themeDisplay.getI18nPath() + layoutFriendlyURL;
1346 }
1347
1348 return virtualHost + _pathContext + layoutFriendlyURL;
1349 }
1350 }
1351 else {
1352 if ((layoutSet.getLayoutSetId() != curLayoutSetId) &&
1353 (layout.getGroup().getClassPK() !=
1354 themeDisplay.getUserId())) {
1355
1356 virtualHost = themeDisplay.getCompany().getVirtualHost();
1357
1358 if (!virtualHost.equalsIgnoreCase(_LOCALHOST)) {
1359 portalURL = getPortalURL(
1360 virtualHost, themeDisplay.getServerPort(),
1361 themeDisplay.isSecure());
1362 }
1363 }
1364 }
1365 }
1366
1367 Group group = layout.getGroup();
1368
1369 String friendlyURL = null;
1370
1371 if (layout.isPrivateLayout()) {
1372 if (group.isUser()) {
1373 friendlyURL = _PRIVATE_USER_SERVLET_MAPPING;
1374 }
1375 else {
1376 friendlyURL = _PRIVATE_GROUP_SERVLET_MAPPING;
1377 }
1378 }
1379 else {
1380 friendlyURL = _PUBLIC_GROUP_SERVLET_MAPPING;
1381 }
1382
1383 if (themeDisplay.isWidget()) {
1384 friendlyURL = PropsValues.WIDGET_SERVLET_MAPPING + friendlyURL;
1385 }
1386
1387 if (themeDisplay.isI18n()) {
1388 friendlyURL = themeDisplay.getI18nPath() + friendlyURL;
1389 }
1390
1391 return portalURL + _pathContext + friendlyURL + group.getFriendlyURL() +
1392 layoutFriendlyURL;
1393 }
1394
1395 public String getLayoutFriendlyURL(
1396 Layout layout, ThemeDisplay themeDisplay, Locale locale) {
1397
1398 String i18nLanguageId = themeDisplay.getI18nLanguageId();
1399 String i18nPath = themeDisplay.getI18nPath();
1400
1401 try {
1402 String tempI18nLanguageId = null;
1403
1404 if ((I18nFilter.getLanguageIds().contains(locale.toString())) &&
1405 ((PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 1) &&
1406 (!locale.equals(LocaleUtil.getDefault()))) ||
1407 (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 2)) {
1408
1409 tempI18nLanguageId = locale.toString();
1410 }
1411
1412 String tempI18nPath = null;
1413
1414 if (Validator.isNotNull(tempI18nLanguageId)) {
1415 tempI18nPath = StringPool.SLASH + tempI18nLanguageId;
1416
1417 if (!LanguageUtil.isDuplicateLanguageCode(
1418 locale.getLanguage())) {
1419
1420 tempI18nPath = StringPool.SLASH + locale.getLanguage();
1421 }
1422 else {
1423 Locale priorityLocale = LanguageUtil.getLocale(
1424 locale.getLanguage());
1425
1426 if (locale.equals(priorityLocale)) {
1427 tempI18nPath = StringPool.SLASH + locale.getLanguage();
1428 }
1429 }
1430 }
1431
1432 themeDisplay.setI18nLanguageId(tempI18nLanguageId);
1433 themeDisplay.setI18nPath(tempI18nPath);
1434
1435 return getLayoutFriendlyURL(layout, themeDisplay);
1436 }
1437 finally {
1438 themeDisplay.setI18nLanguageId(i18nLanguageId);
1439 themeDisplay.setI18nPath(i18nPath);
1440 }
1441 }
1442
1443 public String getLayoutFullURL(Layout layout, ThemeDisplay themeDisplay) {
1444 return getLayoutFullURL(layout, themeDisplay, true);
1445 }
1446
1447 public String getLayoutFullURL(
1448 Layout layout, ThemeDisplay themeDisplay, boolean doAsUser) {
1449
1450 String layoutURL = getLayoutURL(layout, themeDisplay, doAsUser);
1451 String portalURL = themeDisplay.getPortalURL();
1452
1453 if (StringUtil.startsWith(layoutURL, portalURL)) {
1454 return layoutURL;
1455 }
1456 else {
1457 return portalURL + layoutURL;
1458 }
1459 }
1460
1461 public String getLayoutFullURL(long groupId, String portletId)
1462 throws PortalException, SystemException {
1463
1464 long plid = getPlidFromPortletId(groupId, portletId);
1465
1466 if (plid == LayoutConstants.DEFAULT_PLID) {
1467 return null;
1468 }
1469
1470 StringBundler sb = new StringBundler(4);
1471
1472 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1473
1474 Company company = CompanyLocalServiceUtil.getCompany(
1475 layout.getCompanyId());
1476
1477 Group group = GroupLocalServiceUtil.getGroup(groupId);
1478
1479 String portalURL = getPortalURL(
1480 company.getVirtualHost(), getPortalPort(), false);
1481
1482 sb.append(portalURL);
1483
1484 if (layout.isPrivateLayout()) {
1485 if (group.isUser()) {
1486 sb.append(PortalUtil.getPathFriendlyURLPrivateUser());
1487 }
1488 else {
1489 sb.append(PortalUtil.getPathFriendlyURLPrivateGroup());
1490 }
1491 }
1492 else {
1493 sb.append(PortalUtil.getPathFriendlyURLPublic());
1494 }
1495
1496 sb.append(group.getFriendlyURL());
1497 sb.append(layout.getFriendlyURL());
1498
1499 return sb.toString();
1500 }
1501
1502 public String getLayoutFullURL(ThemeDisplay themeDisplay) {
1503 return getLayoutFullURL(themeDisplay.getLayout(), themeDisplay);
1504 }
1505
1506 public String getLayoutSetFriendlyURL(
1507 LayoutSet layoutSet, ThemeDisplay themeDisplay)
1508 throws PortalException, SystemException {
1509
1510 String virtualHost = layoutSet.getVirtualHost();
1511
1512 if (Validator.isNull(virtualHost) &&
1513 Validator.isNotNull(
1514 PropsValues.VIRTUAL_HOSTS_DEFAULT_COMMUNITY_NAME)) {
1515
1516 try {
1517 Group group = GroupLocalServiceUtil.getGroup(
1518 themeDisplay.getCompanyId(),
1519 PropsValues.VIRTUAL_HOSTS_DEFAULT_COMMUNITY_NAME);
1520
1521 if (layoutSet.getGroupId() == group.getGroupId()) {
1522 Company company = themeDisplay.getCompany();
1523
1524 virtualHost = company.getVirtualHost();
1525 }
1526 }
1527 catch (Exception e) {
1528 _log.error(e, e);
1529 }
1530 }
1531
1532 if (Validator.isNotNull(virtualHost)) {
1533 String portalURL = getPortalURL(
1534 virtualHost, themeDisplay.getServerPort(),
1535 themeDisplay.isSecure());
1536
1537
1540 long curLayoutSetId =
1541 themeDisplay.getLayout().getLayoutSet().getLayoutSetId();
1542
1543 if ((layoutSet.getLayoutSetId() != curLayoutSetId) ||
1544 (portalURL.startsWith(themeDisplay.getURLPortal()))) {
1545
1546 String layoutSetFriendlyURL = StringPool.BLANK;
1547
1548 if (themeDisplay.isI18n()) {
1549 layoutSetFriendlyURL = themeDisplay.getI18nPath();
1550 }
1551
1552 return portalURL + _pathContext + layoutSetFriendlyURL;
1553 }
1554 }
1555
1556 Group group = GroupLocalServiceUtil.getGroup(layoutSet.getGroupId());
1557
1558 String friendlyURL = null;
1559
1560 if (layoutSet.isPrivateLayout()) {
1561 if (group.isUser()) {
1562 friendlyURL = _PRIVATE_USER_SERVLET_MAPPING;
1563 }
1564 else {
1565 friendlyURL = _PRIVATE_GROUP_SERVLET_MAPPING;
1566 }
1567 }
1568 else {
1569 friendlyURL = _PUBLIC_GROUP_SERVLET_MAPPING;
1570 }
1571
1572 if (themeDisplay.isI18n()) {
1573 friendlyURL = themeDisplay.getI18nPath() + friendlyURL;
1574 }
1575
1576 return _pathContext + friendlyURL + group.getFriendlyURL();
1577 }
1578
1579 public String getLayoutTarget(Layout layout) {
1580 UnicodeProperties typeSettingsProps =
1581 layout.getTypeSettingsProperties();
1582
1583 String target = typeSettingsProps.getProperty("target");
1584
1585 if (Validator.isNull(target)) {
1586 target = StringPool.BLANK;
1587 }
1588 else {
1589 target = "target=\"" + target + "\"";
1590 }
1591
1592 return target;
1593 }
1594
1595 public String getLayoutURL(Layout layout, ThemeDisplay themeDisplay) {
1596 return getLayoutURL(layout, themeDisplay, true);
1597 }
1598
1599 public String getLayoutURL(
1600 Layout layout, ThemeDisplay themeDisplay, boolean doAsUser) {
1601
1602 if (layout == null) {
1603 return themeDisplay.getPathMain() + PATH_PORTAL_LAYOUT;
1604 }
1605
1606 if (!layout.getType().equals(LayoutConstants.TYPE_URL)) {
1607 String layoutFriendlyURL = getLayoutFriendlyURL(
1608 layout, themeDisplay);
1609
1610 if (Validator.isNotNull(layoutFriendlyURL)) {
1611 if (doAsUser &&
1612 Validator.isNotNull(themeDisplay.getDoAsUserId())) {
1613
1614 layoutFriendlyURL = HttpUtil.addParameter(
1615 layoutFriendlyURL, "doAsUserId",
1616 themeDisplay.getDoAsUserId());
1617 }
1618
1619 return layoutFriendlyURL;
1620 }
1621 }
1622
1623 String layoutURL = getLayoutActualURL(layout);
1624
1625 if (doAsUser && Validator.isNotNull(themeDisplay.getDoAsUserId())) {
1626 layoutURL = HttpUtil.addParameter(
1627 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
1628 }
1629
1630 return layoutURL;
1631 }
1632
1633 public String getLayoutURL(ThemeDisplay themeDisplay) {
1634 return getLayoutURL(themeDisplay.getLayout(), themeDisplay);
1635 }
1636
1637 public String getLayoutViewPage(Layout layout) {
1638 return PropsUtil.get(
1639 PropsKeys.LAYOUT_VIEW_PAGE, new Filter(layout.getType()));
1640 }
1641
1642 public Locale getLocale(HttpServletRequest request) {
1643 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
1644 WebKeys.THEME_DISPLAY);
1645
1646 if (themeDisplay != null) {
1647 return themeDisplay.getLocale();
1648 }
1649 else {
1650 HttpSession session = request.getSession();
1651
1652 return (Locale)session.getAttribute(Globals.LOCALE_KEY);
1653 }
1654 }
1655
1656 public Locale getLocale(RenderRequest renderRequest) {
1657 return getLocale(getHttpServletRequest(renderRequest));
1658 }
1659
1660 public String getNetvibesURL(
1661 Portlet portlet, ThemeDisplay themeDisplay) {
1662
1663 return _getServletURL(
1664 portlet, PropsValues.NETVIBES_SERVLET_MAPPING, themeDisplay);
1665 }
1666
1667 public HttpServletRequest getOriginalServletRequest(
1668 HttpServletRequest request) {
1669
1670 HttpServletRequest originalRequest = request;
1671
1672 while (originalRequest.getClass().getName().startsWith(
1673 "com.liferay.")) {
1674
1675
1678 originalRequest = (HttpServletRequest)
1679 ((HttpServletRequestWrapper)originalRequest).getRequest();
1680 }
1681
1682 return originalRequest;
1683 }
1684
1685 public String getPathContext() {
1686 return _pathContext;
1687 }
1688
1689 public String getPathFriendlyURLPrivateGroup() {
1690 return _pathFriendlyURLPrivateGroup;
1691 }
1692
1693 public String getPathFriendlyURLPrivateUser() {
1694 return _pathFriendlyURLPrivateUser;
1695 }
1696
1697 public String getPathFriendlyURLPublic() {
1698 return _pathFriendlyURLPublic;
1699 }
1700
1701 public String getPathImage() {
1702 return _pathImage;
1703 }
1704
1705 public String getPathMain() {
1706 return _pathMain;
1707 }
1708
1709 public long getPlidFromFriendlyURL(long companyId, String friendlyURL) {
1710 if (Validator.isNull(friendlyURL)) {
1711 return LayoutConstants.DEFAULT_PLID;
1712 }
1713
1714 String[] urlParts = friendlyURL.split("\\/", 4);
1715
1716 if ((friendlyURL.charAt(0) != CharPool.SLASH) &&
1717 (urlParts.length != 4)) {
1718
1719 return LayoutConstants.DEFAULT_PLID;
1720 }
1721
1722 boolean privateLayout = true;
1723
1724 String urlPrefix = StringPool.SLASH + urlParts[1];
1725
1726 if (_PUBLIC_GROUP_SERVLET_MAPPING.equals(urlPrefix)) {
1727 privateLayout = false;
1728 }
1729 else if (_PRIVATE_GROUP_SERVLET_MAPPING.equals(urlPrefix) ||
1730 _PRIVATE_USER_SERVLET_MAPPING.equals(urlPrefix)) {
1731
1732 privateLayout = true;
1733 }
1734 else {
1735 return LayoutConstants.DEFAULT_PLID;
1736 }
1737
1738 Group group = null;
1739
1740 try {
1741 group = GroupLocalServiceUtil.getFriendlyURLGroup(
1742 companyId, StringPool.SLASH + urlParts[2]);
1743 }
1744 catch (Exception e) {
1745 }
1746
1747 if (group != null) {
1748 Layout layout = null;
1749
1750 try {
1751 String layoutFriendlyURL = null;
1752
1753 if (urlParts.length == 4) {
1754 layoutFriendlyURL = StringPool.SLASH + urlParts[3];
1755 }
1756 else {
1757 layoutFriendlyURL = "/1";
1758 }
1759
1760 layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
1761 group.getGroupId(), privateLayout, layoutFriendlyURL);
1762
1763 return layout.getPlid();
1764 }
1765 catch (Exception e) {
1766 }
1767 }
1768
1769 return LayoutConstants.DEFAULT_PLID;
1770 }
1771
1772 public long getPlidFromPortletId(
1773 long groupId, boolean privateLayout, String portletId) {
1774
1775 long plid = LayoutConstants.DEFAULT_PLID;
1776
1777 StringBundler sb = new StringBundler(5);
1778
1779 sb.append(groupId);
1780 sb.append(StringPool.SPACE);
1781 sb.append(privateLayout);
1782 sb.append(StringPool.SPACE);
1783 sb.append(portletId);
1784
1785 String key = sb.toString();
1786
1787 Long plidObj = _plidToPortletIdCache.get(key);
1788
1789 if (plidObj == null) {
1790 plid = _getPlidFromPortletId(groupId, privateLayout, portletId);
1791
1792 if (plid != LayoutConstants.DEFAULT_PLID) {
1793 _plidToPortletIdCache.put(key, plid);
1794 }
1795 }
1796 else {
1797 plid = plidObj.longValue();
1798
1799 boolean validPlid = false;
1800
1801 try {
1802 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1803
1804 LayoutTypePortlet layoutTypePortlet =
1805 (LayoutTypePortlet)layout.getLayoutType();
1806
1807 if (layoutTypePortlet.hasPortletId(portletId)) {
1808 validPlid = true;
1809 }
1810 }
1811 catch (Exception e) {
1812 }
1813
1814 if (!validPlid) {
1815 _plidToPortletIdCache.remove(key);
1816
1817 plid = _getPlidFromPortletId(groupId, privateLayout, portletId);
1818
1819 if (plid != LayoutConstants.DEFAULT_PLID) {
1820 _plidToPortletIdCache.put(key, plid);
1821 }
1822 }
1823 }
1824
1825 return plid;
1826 }
1827
1828 public long getPlidFromPortletId(long groupId, String portletId) {
1829 long plid = getPlidFromPortletId(groupId, false, portletId);
1830
1831 if (plid != 0) {
1832 return plid;
1833 }
1834
1835 return getPlidFromPortletId(groupId, true, portletId);
1836 }
1837
1838 public String getPortalLibDir() {
1839 return _portalLibDir;
1840 }
1841
1842 public int getPortalPort() {
1843 return _portalPort.intValue();
1844 }
1845
1846 public Properties getPortalProperties() {
1847 return PropsUtil.getProperties();
1848 }
1849
1850 public String getPortalURL(HttpServletRequest request) {
1851 return getPortalURL(request, request.isSecure());
1852 }
1853
1854 public String getPortalURL(HttpServletRequest request, boolean secure) {
1855 return getPortalURL(
1856 request.getServerName(), request.getServerPort(), secure);
1857 }
1858
1859 public String getPortalURL(PortletRequest portletRequest) {
1860 return getPortalURL(portletRequest, portletRequest.isSecure());
1861 }
1862
1863 public String getPortalURL(PortletRequest portletRequest, boolean secure) {
1864 return getPortalURL(
1865 portletRequest.getServerName(), portletRequest.getServerPort(),
1866 secure);
1867 }
1868
1869 public String getPortalURL(
1870 String serverName, int serverPort, boolean secure) {
1871
1872 StringBundler sb = new StringBundler();
1873
1874 if (secure || Http.HTTPS.equals(PropsValues.WEB_SERVER_PROTOCOL)) {
1875 sb.append(Http.HTTPS_WITH_SLASH);
1876 }
1877 else {
1878 sb.append(Http.HTTP_WITH_SLASH);
1879 }
1880
1881 if (Validator.isNull(PropsValues.WEB_SERVER_HOST)) {
1882 sb.append(serverName);
1883 }
1884 else {
1885 sb.append(PropsValues.WEB_SERVER_HOST);
1886 }
1887
1888 if (!secure) {
1889 if (PropsValues.WEB_SERVER_HTTP_PORT == -1) {
1890 if ((serverPort != Http.HTTP_PORT) &&
1891 (serverPort != Http.HTTPS_PORT)) {
1892
1893 sb.append(StringPool.COLON);
1894 sb.append(serverPort);
1895 }
1896 }
1897 else {
1898 if ((PropsValues.WEB_SERVER_HTTP_PORT != serverPort) &&
1899 (PropsValues.WEB_SERVER_HTTP_PORT != Http.HTTP_PORT)) {
1900
1901 sb.append(StringPool.COLON);
1902 sb.append(PropsValues.WEB_SERVER_HTTP_PORT);
1903 }
1904 }
1905 }
1906
1907 if (secure) {
1908 if (PropsValues.WEB_SERVER_HTTPS_PORT == -1) {
1909 if ((serverPort != Http.HTTP_PORT) &&
1910 (serverPort != Http.HTTPS_PORT)) {
1911
1912 sb.append(StringPool.COLON);
1913 sb.append(serverPort);
1914 }
1915 }
1916 else {
1917 if ((PropsValues.WEB_SERVER_HTTPS_PORT != serverPort) &&
1918 (PropsValues.WEB_SERVER_HTTPS_PORT != Http.HTTPS_PORT)) {
1919
1920 sb.append(StringPool.COLON);
1921 sb.append(PropsValues.WEB_SERVER_HTTPS_PORT);
1922 }
1923 }
1924 }
1925
1926 return sb.toString();
1927 }
1928
1929 public String getPortalURL(ThemeDisplay themeDisplay) {
1930 String serverName = themeDisplay.getServerName();
1931
1932 Layout layout = themeDisplay.getLayout();
1933
1934 if (layout != null) {
1935 LayoutSet layoutSet = layout.getLayoutSet();
1936
1937 String virtualHost = layoutSet.getVirtualHost();
1938
1939 if (Validator.isNotNull(virtualHost)) {
1940 serverName = virtualHost;
1941 }
1942 }
1943
1944 return getPortalURL(
1945 serverName, themeDisplay.getServerPort(), themeDisplay.isSecure());
1946 }
1947
1948 public String getPortalWebDir() {
1949 return _portalWebDir;
1950 }
1951
1952 public String getPortletDescription(
1953 Portlet portlet, ServletContext servletContext, Locale locale) {
1954
1955 PortletConfig portletConfig = PortletConfigFactory.create(
1956 portlet, servletContext);
1957
1958 ResourceBundle resourceBundle = portletConfig.getResourceBundle(locale);
1959
1960 return resourceBundle.getString(
1961 JavaConstants.JAVAX_PORTLET_DESCRIPTION);
1962 }
1963
1964 public String getPortletDescription(Portlet portlet, User user) {
1965 return getPortletDescription(portlet.getPortletId(), user);
1966 }
1967
1968 public String getPortletDescription(String portletId, Locale locale) {
1969 return LanguageUtil.get(
1970 locale,
1971 JavaConstants.JAVAX_PORTLET_DESCRIPTION.concat(
1972 StringPool.PERIOD).concat(portletId));
1973 }
1974
1975 public String getPortletDescription(String portletId, String languageId) {
1976 Locale locale = LocaleUtil.fromLanguageId(languageId);
1977
1978 return getPortletDescription(portletId, locale);
1979 }
1980
1981 public String getPortletDescription(String portletId, User user) {
1982 return LanguageUtil.get(
1983 user.getLocale(),
1984 JavaConstants.JAVAX_PORTLET_DESCRIPTION.concat(
1985 StringPool.PERIOD).concat(portletId));
1986 }
1987
1988 public Object[] getPortletFriendlyURLMapper(
1989 long groupId, boolean privateLayout, String url)
1990 throws PortalException, SystemException {
1991
1992 return getPortletFriendlyURLMapper(groupId, privateLayout, url, null);
1993 }
1994
1995 public Object[] getPortletFriendlyURLMapper(
1996 long groupId, boolean privateLayout, String url,
1997 Map<String, String[]> params)
1998 throws PortalException, SystemException {
1999
2000 boolean foundFriendlyURLMapper = false;
2001
2002 String friendlyURL = url;
2003 String queryString = StringPool.BLANK;
2004
2005 List<Portlet> portlets =
2006 PortletLocalServiceUtil.getFriendlyURLMapperPortlets();
2007
2008 Iterator<Portlet> itr = portlets.iterator();
2009
2010 while (itr.hasNext()) {
2011 Portlet portlet = itr.next();
2012
2013 FriendlyURLMapper friendlyURLMapper =
2014 portlet.getFriendlyURLMapperInstance();
2015
2016 if (url.endsWith(
2017 StringPool.SLASH + friendlyURLMapper.getMapping())) {
2018
2019 url += StringPool.SLASH;
2020 }
2021
2022 int pos = -1;
2023
2024 if (friendlyURLMapper.isCheckMappingWithPrefix()) {
2025 pos = url.indexOf(
2026 FRIENDLY_URL_SEPARATOR + friendlyURLMapper.getMapping() +
2027 StringPool.SLASH);
2028 }
2029 else {
2030 pos = url.indexOf(
2031 StringPool.SLASH + friendlyURLMapper.getMapping() +
2032 StringPool.SLASH);
2033 }
2034
2035 if (pos != -1) {
2036 foundFriendlyURLMapper = true;
2037
2038 friendlyURL = url.substring(0, pos);
2039
2040 Map<String, String[]> actualParams =
2041 new HashMap<String, String[]>();
2042
2043
2057
2058 Map<String, String> prpIdentifiers =
2059 new HashMap<String, String>();
2060
2061 Set<PublicRenderParameter> publicRenderParameters =
2062 portlet.getPublicRenderParameters();
2063
2064 for (PublicRenderParameter publicRenderParameter :
2065 publicRenderParameters) {
2066
2067 QName qName = publicRenderParameter.getQName();
2068
2069 String publicRenderParameterIdentifier =
2070 qName.getLocalPart();
2071 String publicRenderParameterName =
2072 QNameUtil.getPublicRenderParameterName(qName);
2073
2074 prpIdentifiers.put(
2075 publicRenderParameterIdentifier,
2076 publicRenderParameterName);
2077 }
2078
2079 FriendlyURLMapperThreadLocal.setPRPIdentifiers(prpIdentifiers);
2080
2081 if (friendlyURLMapper.isCheckMappingWithPrefix()) {
2082 friendlyURLMapper.populateParams(
2083 url.substring(pos + 2), actualParams);
2084 }
2085 else {
2086 friendlyURLMapper.populateParams(
2087 url.substring(pos), actualParams);
2088 }
2089
2090 queryString =
2091 StringPool.AMPERSAND +
2092 HttpUtil.parameterMapToString(actualParams, false);
2093
2094 break;
2095 }
2096 }
2097
2098 if (!foundFriendlyURLMapper) {
2099 int x = url.indexOf(FRIENDLY_URL_SEPARATOR);
2100
2101 if (x != -1) {
2102 int y = url.indexOf(StringPool.SLASH, x + 3);
2103
2104 if (y == -1) {
2105 y = url.length();
2106 }
2107
2108 String ppid = url.substring(x + 3, y);
2109
2110 if (Validator.isNotNull(ppid)) {
2111 friendlyURL = url.substring(0, x);
2112
2113 Map<String, String[]> actualParams = null;
2114
2115 if (params != null) {
2116 actualParams = new HashMap<String, String[]>(params);
2117 }
2118 else {
2119 actualParams = new HashMap<String, String[]>();
2120 }
2121
2122 actualParams.put("p_p_id", new String[] {ppid});
2123 actualParams.put("p_p_lifecycle", new String[] {"0"});
2124 actualParams.put(
2125 "p_p_state",
2126 new String[] {WindowState.MAXIMIZED.toString()});
2127 actualParams.put(
2128 "p_p_mode", new String[] {PortletMode.VIEW.toString()});
2129
2130 queryString =
2131 StringPool.AMPERSAND +
2132 HttpUtil.parameterMapToString(actualParams, false);
2133 }
2134 }
2135 }
2136
2137 friendlyURL = StringUtil.replace(
2138 friendlyURL, StringPool.DOUBLE_SLASH, StringPool.SLASH);
2139
2140 if (friendlyURL.endsWith(StringPool.SLASH)) {
2141 friendlyURL = friendlyURL.substring(0, friendlyURL.length() - 1);
2142 }
2143
2144 Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
2145 groupId, privateLayout, friendlyURL);
2146
2147 return new Object[] {layout, queryString};
2148 }
2149
2150
2153 public long getPortletGroupId(ActionRequest actionRequest) {
2154 return getScopeGroupId(actionRequest);
2155 }
2156
2157
2160 public long getPortletGroupId(HttpServletRequest request) {
2161 return getScopeGroupId(request);
2162 }
2163
2164
2167 public long getPortletGroupId(Layout layout) {
2168 return getScopeGroupId(layout);
2169 }
2170
2171
2174 public long getPortletGroupId(long plid) {
2175 Layout layout = null;
2176
2177 try {
2178 layout = LayoutLocalServiceUtil.getLayout(plid);
2179 }
2180 catch (Exception e) {
2181 }
2182
2183 return getPortletGroupId(layout);
2184 }
2185
2186
2189 public long getPortletGroupId(RenderRequest renderRequest) {
2190 return getScopeGroupId(renderRequest);
2191 }
2192
2193 public String getPortletId(HttpServletRequest request) {
2194 PortletConfigImpl configImpl = (PortletConfigImpl)request.getAttribute(
2195 JavaConstants.JAVAX_PORTLET_CONFIG);
2196
2197 return configImpl.getPortletId();
2198 }
2199
2200 public String getPortletId(PortletRequest portletRequest) {
2201 PortletConfigImpl portletConfigImpl =
2202 (PortletConfigImpl)portletRequest.getAttribute(
2203 JavaConstants.JAVAX_PORTLET_CONFIG);
2204
2205 return portletConfigImpl.getPortletId();
2206 }
2207
2208 public String getPortletNamespace(String portletId) {
2209 return StringPool.UNDERLINE.concat(portletId).concat(
2210 StringPool.UNDERLINE);
2211 }
2212
2213 public String getPortletTitle(Portlet portlet, Locale locale) {
2214 return getPortletTitle(portlet.getPortletId(), locale);
2215 }
2216
2217 public String getPortletTitle(
2218 Portlet portlet, ServletContext servletContext, Locale locale) {
2219
2220 PortletConfig portletConfig = PortletConfigFactory.create(
2221 portlet, servletContext);
2222
2223 ResourceBundle resourceBundle = portletConfig.getResourceBundle(locale);
2224
2225 return resourceBundle.getString(JavaConstants.JAVAX_PORTLET_TITLE);
2226 }
2227
2228 public String getPortletTitle(Portlet portlet, String languageId) {
2229 return getPortletTitle(portlet.getPortletId(), languageId);
2230 }
2231
2232 public String getPortletTitle(Portlet portlet, User user) {
2233 return getPortletTitle(portlet.getPortletId(), user);
2234 }
2235
2236 public String getPortletTitle(String portletId, Locale locale) {
2237 return LanguageUtil.get(
2238 locale,
2239 JavaConstants.JAVAX_PORTLET_TITLE.concat(StringPool.PERIOD).concat(
2240 portletId));
2241 }
2242
2243 public String getPortletTitle(String portletId, String languageId) {
2244 Locale locale = LocaleUtil.fromLanguageId(languageId);
2245
2246 return getPortletTitle(portletId, locale);
2247 }
2248
2249 public String getPortletTitle(String portletId, User user) {
2250 return LanguageUtil.get(
2251 user.getLocale(),
2252 JavaConstants.JAVAX_PORTLET_TITLE.concat(StringPool.PERIOD).concat(
2253 portletId));
2254 }
2255
2256 public String getPortletXmlFileName() throws SystemException {
2257 if (PrefsPropsUtil.getBoolean(
2258 PropsKeys.AUTO_DEPLOY_CUSTOM_PORTLET_XML,
2259 PropsValues.AUTO_DEPLOY_CUSTOM_PORTLET_XML)) {
2260
2261 return PORTLET_XML_FILE_NAME_CUSTOM;
2262 }
2263 else {
2264 return PORTLET_XML_FILE_NAME_STANDARD;
2265 }
2266 }
2267
2268 public PortletPreferences getPreferences(HttpServletRequest request) {
2269 RenderRequest renderRequest = (RenderRequest)request.getAttribute(
2270 JavaConstants.JAVAX_PORTLET_REQUEST);
2271
2272 PortletPreferences prefs = null;
2273
2274 if (renderRequest != null) {
2275 PortletPreferencesWrapper prefsWrapper =
2276 (PortletPreferencesWrapper)renderRequest.getPreferences();
2277
2278 prefs = prefsWrapper.getPreferencesImpl();
2279 }
2280
2281 return prefs;
2282 }
2283
2284 public PreferencesValidator getPreferencesValidator(Portlet portlet) {
2285 PortletApp portletApp = portlet.getPortletApp();
2286
2287 if (portletApp.isWARFile()) {
2288 PortletBag portletBag = PortletBagPool.get(
2289 portlet.getRootPortletId());
2290
2291 return portletBag.getPreferencesValidatorInstance();
2292 }
2293 else {
2294 PreferencesValidator prefsValidator = null;
2295
2296 if (Validator.isNotNull(portlet.getPreferencesValidator())) {
2297 prefsValidator =
2298 (PreferencesValidator)InstancePool.get(
2299 portlet.getPreferencesValidator());
2300 }
2301
2302 return prefsValidator;
2303 }
2304 }
2305
2306 public long getScopeGroupId(HttpServletRequest request) {
2307 Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
2308
2309 return getScopeGroupId(layout);
2310 }
2311
2312 public long getScopeGroupId(Layout layout) {
2313 if (layout == null) {
2314 return 0;
2315 }
2316 else {
2317 return layout.getGroupId();
2318 }
2319 }
2320
2321 public long getScopeGroupId(long plid) {
2322 Layout layout = null;
2323
2324 try {
2325 layout = LayoutLocalServiceUtil.getLayout(plid);
2326 }
2327 catch (Exception e) {
2328 }
2329
2330 return getScopeGroupId(layout);
2331 }
2332
2333 public long getScopeGroupId(PortletRequest portletRequest) {
2334 return getScopeGroupId(getHttpServletRequest(portletRequest));
2335 }
2336
2337 public User getSelectedUser(HttpServletRequest request)
2338 throws PortalException, RemoteException, SystemException {
2339
2340 return getSelectedUser(request, true);
2341 }
2342
2343 public User getSelectedUser(
2344 HttpServletRequest request, boolean checkPermission)
2345 throws PortalException, RemoteException, SystemException {
2346
2347 long userId = ParamUtil.getLong(request, "p_u_i_d");
2348
2349 User user = null;
2350
2351 try {
2352 if (checkPermission) {
2353 user = UserServiceUtil.getUserById(userId);
2354 }
2355 else {
2356 user = UserLocalServiceUtil.getUserById(userId);
2357 }
2358 }
2359 catch (NoSuchUserException nsue) {
2360 }
2361
2362 return user;
2363 }
2364
2365 public User getSelectedUser(PortletRequest portletRequest)
2366 throws PortalException, RemoteException, SystemException {
2367
2368 return getSelectedUser(portletRequest, true);
2369 }
2370
2371 public User getSelectedUser(
2372 PortletRequest portletRequest, boolean checkPermission)
2373 throws PortalException, RemoteException, SystemException {
2374
2375 return getSelectedUser(
2376 getHttpServletRequest(portletRequest), checkPermission);
2377 }
2378
2379 public ServletContext getServletContext(
2380 Portlet portlet, ServletContext servletContext) {
2381
2382 PortletConfig portletConfig = PortletConfigFactory.create(
2383 portlet, servletContext);
2384
2385 PortletContextImpl portletContextImpl =
2386 (PortletContextImpl)portletConfig.getPortletContext();
2387
2388 return portletContextImpl.getServletContext();
2389 }
2390
2391 public String getStaticResourceURL(
2392 HttpServletRequest request, String uri) {
2393
2394 return getStaticResourceURL(request, uri, null, 0);
2395 }
2396
2397 public String getStaticResourceURL(
2398 HttpServletRequest request, String uri, long timestamp) {
2399
2400 return getStaticResourceURL(request, uri, null, timestamp);
2401 }
2402
2403 public String getStaticResourceURL(
2404 HttpServletRequest request, String uri, String queryString) {
2405
2406 return getStaticResourceURL(request, uri, queryString, 0);
2407 }
2408
2409 public String getStaticResourceURL(
2410 HttpServletRequest request, String uri, String queryString,
2411 long timestamp) {
2412
2413 if (uri.contains(StringPool.QUESTION)) {
2414 return uri;
2415 }
2416
2417 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
2418 WebKeys.THEME_DISPLAY);
2419
2420 Theme theme = themeDisplay.getTheme();
2421 ColorScheme colorScheme = themeDisplay.getColorScheme();
2422
2423 Map<String, String[]> parameterMap = null;
2424
2425 if (Validator.isNotNull(queryString)) {
2426 parameterMap = HttpUtil.getParameterMap(queryString);
2427 }
2428
2429 StringBundler sb = new StringBundler();
2430
2431
2433 sb.append(uri);
2434 sb.append(StringPool.QUESTION);
2435
2436
2438 if ((parameterMap == null) ||
2439 (!parameterMap.containsKey("browserId"))) {
2440
2441 sb.append("&browserId=");
2442 sb.append(BrowserSnifferUtil.getBrowserId(request));
2443 }
2444
2445
2447 if (uri.endsWith(".jsp")) {
2448 if ((parameterMap == null) ||
2449 (!parameterMap.containsKey("themeId"))) {
2450
2451 sb.append("&themeId=");
2452 sb.append(theme.getThemeId());
2453 }
2454
2455 if ((parameterMap == null) ||
2456 (!parameterMap.containsKey("colorSchemeId"))) {
2457
2458 sb.append("&colorSchemeId=");
2459 sb.append(colorScheme.getColorSchemeId());
2460 }
2461 }
2462
2463
2465 if ((parameterMap == null) ||
2466 (!parameterMap.containsKey("minifierType"))) {
2467
2468 String minifierType = StringPool.BLANK;
2469
2470 if (uri.endsWith(".css") || uri.endsWith("css.jsp")) {
2471 if (themeDisplay.isThemeCssFastLoad()) {
2472 minifierType = "css";
2473 }
2474 }
2475 else if (themeDisplay.isThemeJsFastLoad()) {
2476 minifierType = "js";
2477 }
2478
2479 if (Validator.isNotNull(minifierType)) {
2480 sb.append("&minifierType=");
2481 sb.append(minifierType);
2482 }
2483 }
2484
2485
2487 if (Validator.isNotNull(queryString)) {
2488 if (!queryString.startsWith(StringPool.AMPERSAND)) {
2489 sb.append(StringPool.AMPERSAND);
2490 }
2491
2492 sb.append(queryString);
2493 }
2494
2495
2497 sb.append("&languageId=");
2498 sb.append(themeDisplay.getLanguageId());
2499
2500
2502 boolean appendedTimestamp = false;
2503
2504 if (uri.startsWith(StrutsUtil.TEXT_HTML_DIR)) {
2505 ServletContext servletContext =
2506 (ServletContext)request.getAttribute(WebKeys.CTX);
2507
2508 String uriRealPath = ServletContextUtil.getRealPath(
2509 servletContext, uri);
2510
2511 if (uriRealPath != null) {
2512 File uriFile = new File(uriRealPath);
2513
2514 if (uriFile.exists()) {
2515 sb.append("&t=");
2516 sb.append(uriFile.lastModified());
2517
2518 appendedTimestamp = true;
2519 }
2520 }
2521 }
2522
2523 if (!appendedTimestamp &&
2524 ((parameterMap == null) || !parameterMap.containsKey("t"))) {
2525
2526 sb.append("&t=");
2527 sb.append(theme.getTimestamp());
2528 }
2529
2530 String url = sb.toString();
2531
2532 url = StringUtil.replace(url, "?&", StringPool.QUESTION);
2533
2534 return url;
2535 }
2536
2537 public String getStrutsAction(HttpServletRequest request) {
2538 String strutsAction = ParamUtil.getString(request, "struts_action");
2539
2540 if (Validator.isNotNull(strutsAction)) {
2541
2542
2546 return StringPool.BLANK;
2547 }
2548
2549 return _getPortletParam(request, "struts_action");
2550 }
2551
2552 public String[] getSystemCommunityRoles() {
2553 return _allSystemCommunityRoles;
2554 }
2555
2556 public String[] getSystemGroups() {
2557 return _allSystemGroups;
2558 }
2559
2560 public String[] getSystemOrganizationRoles() {
2561 return _allSystemOrganizationRoles;
2562 }
2563
2564 public String[] getSystemRoles() {
2565 return _allSystemRoles;
2566 }
2567
2568 public UploadServletRequest getUploadServletRequest(
2569 HttpServletRequest request) {
2570
2571 HttpServletRequestWrapper requestWrapper = null;
2572
2573 if (request instanceof HttpServletRequestWrapper) {
2574 requestWrapper = (HttpServletRequestWrapper)request;
2575 }
2576
2577 UploadServletRequest uploadRequest = null;
2578
2579 while (uploadRequest == null) {
2580
2581
2584 if (requestWrapper instanceof UploadServletRequest) {
2585 uploadRequest = (UploadServletRequest)requestWrapper;
2586 }
2587 else {
2588 HttpServletRequest parentRequest =
2589 (HttpServletRequest)requestWrapper.getRequest();
2590
2591 if (!(parentRequest instanceof HttpServletRequestWrapper)) {
2592
2593
2596 uploadRequest = new UploadServletRequestImpl(parentRequest);
2597
2598 break;
2599 }
2600 else {
2601 requestWrapper = (HttpServletRequestWrapper)parentRequest;
2602 }
2603 }
2604 }
2605
2606 return uploadRequest;
2607 }
2608
2609 public UploadPortletRequest getUploadPortletRequest(
2610 PortletRequest portletRequest) {
2611
2612 PortletRequestImpl portletRequestImpl =
2613 (PortletRequestImpl)portletRequest;
2614
2615 DynamicServletRequest dynamicRequest =
2616 (DynamicServletRequest)portletRequestImpl.getHttpServletRequest();
2617
2618 HttpServletRequestWrapper requestWrapper =
2619 (HttpServletRequestWrapper)dynamicRequest.getRequest();
2620
2621 UploadServletRequest uploadRequest = getUploadServletRequest(
2622 requestWrapper);
2623
2624 return new UploadPortletRequestImpl(
2625 uploadRequest,
2626 PortalUtil.getPortletNamespace(
2627 portletRequestImpl.getPortletName()));
2628 }
2629
2630 public Date getUptime() {
2631 return _UP_TIME;
2632 }
2633
2634 public String getURLWithSessionId(String url, String sessionId) {
2635 if (!PropsValues.SESSION_ENABLE_URL_WITH_SESSION_ID) {
2636 return url;
2637 }
2638
2639
2641 int x = url.indexOf(StringPool.SEMICOLON);
2642
2643 if (x != -1) {
2644 return url;
2645 }
2646
2647 x = url.indexOf(StringPool.QUESTION);
2648
2649 if (x != -1) {
2650 StringBundler sb = new StringBundler(4);
2651
2652 sb.append(url.substring(0, x));
2653 sb.append(_JSESSIONID);
2654 sb.append(sessionId);
2655 sb.append(url.substring(x));
2656
2657 return sb.toString();
2658 }
2659
2660
2663 x = url.indexOf(StringPool.DOUBLE_SLASH);
2664
2665 StringBundler sb = new StringBundler(4);
2666
2667 sb.append(url);
2668
2669 if (x != -1) {
2670 int y = url.lastIndexOf(StringPool.SLASH);
2671
2672 if (x + 1 == y) {
2673 sb.append(StringPool.SLASH);
2674 }
2675 }
2676
2677 sb.append(_JSESSIONID);
2678 sb.append(sessionId);
2679
2680 return sb.toString();
2681 }
2682
2683 public User getUser(HttpServletRequest request)
2684 throws PortalException, SystemException {
2685
2686 long userId = getUserId(request);
2687
2688 if (userId <= 0) {
2689
2690
2695 String remoteUser = request.getRemoteUser();
2696
2697 if (remoteUser == null) {
2698 return null;
2699 }
2700
2701 userId = GetterUtil.getLong(remoteUser);
2702 }
2703
2704 User user = (User)request.getAttribute(WebKeys.USER);
2705
2706 if (user == null) {
2707 user = UserLocalServiceUtil.getUserById(userId);
2708
2709 request.setAttribute(WebKeys.USER, user);
2710 }
2711
2712 return user;
2713 }
2714
2715 public User getUser(PortletRequest portletRequest)
2716 throws PortalException, SystemException {
2717
2718 return getUser(getHttpServletRequest(portletRequest));
2719 }
2720
2721 public long getUserId(HttpServletRequest request) {
2722 Long userIdObj = (Long)request.getAttribute(WebKeys.USER_ID);
2723
2724 if (userIdObj != null) {
2725 return userIdObj.longValue();
2726 }
2727
2728 String path = GetterUtil.getString(request.getPathInfo());
2729 String strutsAction = getStrutsAction(request);
2730 String actionName = _getPortletParam(request, "actionName");
2731
2732 boolean alwaysAllowDoAsUser = false;
2733
2734 if (path.equals("/portal/fckeditor") ||
2735 strutsAction.equals("/document_library/edit_file_entry") ||
2736 strutsAction.equals("/image_gallery/edit_image") ||
2737 strutsAction.equals("/wiki/edit_page_attachment") ||
2738 actionName.equals("addFile")) {
2739
2740 alwaysAllowDoAsUser = true;
2741 }
2742
2743 if ((!PropsValues.PORTAL_JAAS_ENABLE &&
2744 PropsValues.PORTAL_IMPERSONATION_ENABLE) ||
2745 (alwaysAllowDoAsUser)) {
2746
2747 String doAsUserIdString = ParamUtil.getString(
2748 request, "doAsUserId");
2749
2750 try {
2751 long doAsUserId = getDoAsUserId(
2752 request, doAsUserIdString, alwaysAllowDoAsUser);
2753
2754 if (doAsUserId > 0) {
2755 if (_log.isDebugEnabled()) {
2756 _log.debug("Impersonating user " + doAsUserId);
2757 }
2758
2759 return doAsUserId;
2760 }
2761 }
2762 catch (Exception e) {
2763 _log.error("Unable to impersonate user " + doAsUserIdString, e);
2764 }
2765 }
2766
2767 HttpSession session = request.getSession();
2768
2769 userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
2770
2771 if (userIdObj != null) {
2772 request.setAttribute(WebKeys.USER_ID, userIdObj);
2773
2774 return userIdObj.longValue();
2775 }
2776 else {
2777 return 0;
2778 }
2779 }
2780
2781 public long getUserId(PortletRequest portletRequest) {
2782 return getUserId(getHttpServletRequest(portletRequest));
2783 }
2784
2785 public String getUserName(long userId, String defaultUserName) {
2786 return getUserName(
2787 userId, defaultUserName, UserAttributes.USER_NAME_FULL);
2788 }
2789
2790 public String getUserName(
2791 long userId, String defaultUserName, HttpServletRequest request) {
2792
2793 return getUserName(
2794 userId, defaultUserName, UserAttributes.USER_NAME_FULL, request);
2795 }
2796
2797 public String getUserName(
2798 long userId, String defaultUserName, String userAttribute) {
2799
2800 return getUserName(userId, defaultUserName, userAttribute, null);
2801 }
2802
2803 public String getUserName(
2804 long userId, String defaultUserName, String userAttribute,
2805 HttpServletRequest request) {
2806
2807 String userName = defaultUserName;
2808
2809 try {
2810 User user = UserLocalServiceUtil.getUserById(userId);
2811
2812 if (userAttribute.equals(UserAttributes.USER_NAME_FULL)) {
2813 userName = user.getFullName();
2814 }
2815 else {
2816 userName = user.getScreenName();
2817 }
2818
2819 if (request != null) {
2820 Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
2821
2822 PortletURL portletURL = new PortletURLImpl(
2823 request, PortletKeys.DIRECTORY, layout.getPlid(),
2824 PortletRequest.RENDER_PHASE);
2825
2826 portletURL.setWindowState(WindowState.MAXIMIZED);
2827 portletURL.setPortletMode(PortletMode.VIEW);
2828
2829 portletURL.setParameter(
2830 "struts_action", "/directory/edit_user");
2831 portletURL.setParameter(
2832 "p_u_i_d", String.valueOf(user.getUserId()));
2833
2834 userName =
2835 "<a href=\"" + portletURL.toString() + "\">" + userName +
2836 "</a>";
2837 }
2838 }
2839 catch (Exception e) {
2840 }
2841
2842 return userName;
2843 }
2844
2845 public String getUserPassword(HttpServletRequest request) {
2846 HttpSession session = request.getSession();
2847
2848 return getUserPassword(session);
2849 }
2850
2851 public String getUserPassword(HttpSession session) {
2852 return (String)session.getAttribute(WebKeys.USER_PASSWORD);
2853 }
2854
2855 public String getUserPassword(PortletRequest portletRequest) {
2856 return getUserPassword(getHttpServletRequest(portletRequest));
2857 }
2858
2859 public String getUserValue(long userId, String param, String defaultValue)
2860 throws SystemException {
2861
2862 if (Validator.isNotNull(defaultValue)) {
2863 return defaultValue;
2864 }
2865 else {
2866 try {
2867 User user = UserLocalServiceUtil.getUserById(userId);
2868
2869 return BeanPropertiesUtil.getString(user, param, defaultValue);
2870 }
2871 catch (PortalException pe) {
2872 return StringPool.BLANK;
2873 }
2874 }
2875 }
2876
2877 public String getWidgetURL(Portlet portlet, ThemeDisplay themeDisplay) {
2878 return _getServletURL(
2879 portlet, PropsValues.WIDGET_SERVLET_MAPPING, themeDisplay);
2880 }
2881
2882 public boolean isLayoutFriendliable(Layout layout) {
2883 return GetterUtil.getBoolean(
2884 PropsUtil.get(
2885 PropsKeys.LAYOUT_URL_FRIENDLIABLE,
2886 new Filter(layout.getType())),
2887 true);
2888 }
2889
2890 public boolean isLayoutParentable(Layout layout) {
2891 return isLayoutParentable(layout.getType());
2892 }
2893
2894 public boolean isLayoutParentable(String type) {
2895 return GetterUtil.getBoolean(
2896 PropsUtil.get(PropsKeys.LAYOUT_PARENTABLE, new Filter(type)), true);
2897 }
2898
2899 public boolean isLayoutSitemapable(Layout layout) {
2900 if (layout.isPrivateLayout()) {
2901 return false;
2902 }
2903
2904 return GetterUtil.getBoolean(PropsUtil.get(
2905 PropsKeys.LAYOUT_SITEMAPABLE, new Filter(layout.getType())), true);
2906 }
2907
2908 public boolean isMethodGet(PortletRequest portletRequest) {
2909 HttpServletRequest request = getHttpServletRequest(portletRequest);
2910
2911 String method = GetterUtil.getString(request.getMethod());
2912
2913 if (method.equalsIgnoreCase(HttpMethods.GET)) {
2914 return true;
2915 }
2916 else {
2917 return false;
2918 }
2919 }
2920
2921 public boolean isMethodPost(PortletRequest portletRequest) {
2922 HttpServletRequest request = getHttpServletRequest(portletRequest);
2923
2924 String method = GetterUtil.getString(request.getMethod());
2925
2926 if (method.equalsIgnoreCase(HttpMethods.POST)) {
2927 return true;
2928 }
2929 else {
2930 return false;
2931 }
2932 }
2933
2934 public boolean isReservedParameter(String name) {
2935 return _reservedParams.contains(name);
2936 }
2937
2938 public boolean isSystemGroup(String groupName) {
2939 if (groupName == null) {
2940 return false;
2941 }
2942
2943 groupName = groupName.trim();
2944
2945 int pos = Arrays.binarySearch(
2946 _sortedSystemGroups, groupName, new StringComparator());
2947
2948 if (pos >= 0) {
2949 return true;
2950 }
2951 else {
2952 return false;
2953 }
2954 }
2955
2956 public boolean isSystemRole(String roleName) {
2957 if (roleName == null) {
2958 return false;
2959 }
2960
2961 roleName = roleName.trim();
2962
2963 int pos = Arrays.binarySearch(
2964 _sortedSystemRoles, roleName, new StringComparator());
2965
2966 if (pos >= 0) {
2967 return true;
2968 }
2969 else {
2970 pos = Arrays.binarySearch(
2971 _sortedSystemCommunityRoles, roleName, new StringComparator());
2972
2973 if (pos >= 0) {
2974 return true;
2975 }
2976 else {
2977 pos = Arrays.binarySearch(
2978 _sortedSystemOrganizationRoles, roleName,
2979 new StringComparator());
2980
2981 if (pos >= 0) {
2982 return true;
2983 }
2984 }
2985 }
2986
2987 return false;
2988 }
2989
2990 public boolean isUpdateAvailable() throws SystemException {
2991 return PluginPackageUtil.isUpdateAvailable();
2992 }
2993
2994 public void renderPage(
2995 StringBuilder sb, ServletContext servletContext,
2996 HttpServletRequest request, HttpServletResponse response,
2997 String path)
2998 throws IOException, ServletException {
2999
3000 RequestDispatcher requestDispatcher =
3001 servletContext.getRequestDispatcher(path);
3002
3003 StringServletResponse stringResponse = new StringServletResponse(
3004 response);
3005
3006 requestDispatcher.include(request, stringResponse);
3007
3008 sb.append(stringResponse.getString());
3009 }
3010
3011 public void renderPortlet(
3012 StringBuilder sb, ServletContext servletContext,
3013 HttpServletRequest request, HttpServletResponse response,
3014 Portlet portlet, String queryString)
3015 throws IOException, ServletException {
3016
3017 renderPortlet(
3018 sb, servletContext, request, response, portlet, queryString, null,
3019 null, null);
3020 }
3021
3022 public void renderPortlet(
3023 StringBuilder sb, ServletContext servletContext,
3024 HttpServletRequest request, HttpServletResponse response,
3025 Portlet portlet, String queryString, String columnId,
3026 Integer columnPos, Integer columnCount)
3027 throws IOException, ServletException {
3028
3029 renderPortlet(
3030 sb, servletContext, request, response, portlet, queryString,
3031 columnId, columnPos, columnCount, null);
3032 }
3033
3034 public void renderPortlet(
3035 StringBuilder sb, ServletContext servletContext,
3036 HttpServletRequest request, HttpServletResponse response,
3037 Portlet portlet, String queryString, String columnId,
3038 Integer columnPos, Integer columnCount, String path)
3039 throws IOException, ServletException {
3040
3041 queryString = GetterUtil.getString(queryString);
3042 columnId = GetterUtil.getString(columnId);
3043
3044 if (columnPos == null) {
3045 columnPos = new Integer(0);
3046 }
3047
3048 if (columnCount == null) {
3049 columnCount = new Integer(0);
3050 }
3051
3052 request.setAttribute(WebKeys.RENDER_PORTLET, portlet);
3053 request.setAttribute(WebKeys.RENDER_PORTLET_QUERY_STRING, queryString);
3054 request.setAttribute(WebKeys.RENDER_PORTLET_COLUMN_ID, columnId);
3055 request.setAttribute(WebKeys.RENDER_PORTLET_COLUMN_POS, columnPos);
3056 request.setAttribute(WebKeys.RENDER_PORTLET_COLUMN_COUNT, columnCount);
3057
3058 if (path == null) {
3059 path = "/html/portal/render_portlet.jsp";
3060 }
3061
3062 RequestDispatcher requestDispatcher =
3063 servletContext.getRequestDispatcher(path);
3064
3065 if (sb != null) {
3066 StringServletResponse stringResponse = new StringServletResponse(
3067 response);
3068
3069 requestDispatcher.include(request, stringResponse);
3070
3071 sb.append(stringResponse.getString());
3072 }
3073 else {
3074
3075
3077 response.setContentType(ContentTypes.TEXT_HTML_UTF8);
3078
3079 requestDispatcher.include(request, response);
3080 }
3081 }
3082
3083 public void sendError(
3084 Exception e, ActionRequest actionRequest,
3085 ActionResponse actionResponse)
3086 throws IOException {
3087
3088 sendError(0, e, actionRequest, actionResponse);
3089 }
3090
3091 public void sendError(
3092 Exception e, HttpServletRequest request,
3093 HttpServletResponse response)
3094 throws IOException, ServletException {
3095
3096 sendError(0, e, request, response);
3097 }
3098
3099 public void sendError(
3100 int status, Exception e, ActionRequest actionRequest,
3101 ActionResponse actionResponse)
3102 throws IOException {
3103
3104 StringBundler sb = new StringBundler(7);
3105
3106 sb.append(_pathContext);
3107 sb.append("/portal/status?status=");
3108 sb.append(status);
3109 sb.append("&exception=");
3110 sb.append(e.getClass().getName());
3111 sb.append("&previousURL=");
3112 sb.append(HttpUtil.encodeURL(PortalUtil.getCurrentURL(actionRequest)));
3113
3114 actionResponse.sendRedirect(sb.toString());
3115 }
3116
3117 public void sendError(
3118 int status, Exception e, HttpServletRequest request,
3119 HttpServletResponse response)
3120 throws IOException, ServletException {
3121
3122 if (_log.isInfoEnabled()) {
3123 String currentURL = (String)request.getAttribute(
3124 WebKeys.CURRENT_URL);
3125
3126 _log.info(
3127 "Current URL " + currentURL + " generates exception: " +
3128 e.getMessage());
3129 }
3130
3131 if (_log.isWarnEnabled()) {
3132 _log.warn(e, e);
3133 }
3134
3135 if (response.isCommitted()) {
3136 return;
3137 }
3138
3139 if (status == 0) {
3140 if (e instanceof PrincipalException) {
3141 status = HttpServletResponse.SC_FORBIDDEN;
3142 }
3143 else {
3144 String name = e.getClass().getName();
3145
3146 name = name.substring(name.lastIndexOf(StringPool.PERIOD) + 1);
3147
3148 if (name.startsWith("NoSuch") && name.endsWith("Exception")) {
3149 status = HttpServletResponse.SC_NOT_FOUND;
3150 }
3151 }
3152
3153 if (status == 0) {
3154
3155
3157 if (PropsValues.TCK_URL) {
3158 status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
3159 }
3160 else {
3161 status = HttpServletResponse.SC_BAD_REQUEST;
3162 }
3163 }
3164 }
3165
3166 HttpSession session = request.getSession();
3167
3168 ServletContext servletContext = session.getServletContext();
3169
3170 String redirect = PATH_MAIN + "/portal/status";
3171
3172 if (e instanceof NoSuchLayoutException &&
3173 Validator.isNotNull(
3174 PropsValues.LAYOUT_FRIENDLY_URL_PAGE_NOT_FOUND)) {
3175
3176 response.setStatus(status);
3177
3178 redirect = PropsValues.LAYOUT_FRIENDLY_URL_PAGE_NOT_FOUND;
3179
3180 RequestDispatcher requestDispatcher =
3181 servletContext.getRequestDispatcher(redirect);
3182
3183 if (requestDispatcher != null) {
3184 requestDispatcher.forward(request, response);
3185 }
3186 }
3187 else if (PropsValues.LAYOUT_SHOW_HTTP_STATUS) {
3188 response.setStatus(status);
3189
3190 SessionErrors.add(request, e.getClass().getName(), e);
3191
3192 RequestDispatcher requestDispatcher =
3193 servletContext.getRequestDispatcher(redirect);
3194
3195 if (requestDispatcher != null) {
3196 requestDispatcher.forward(request, response);
3197 }
3198 }
3199 else {
3200 if (e != null) {
3201 response.sendError(status, e.getMessage());
3202 }
3203 else {
3204 response.sendError(status);
3205 }
3206 }
3207 }
3208
3209
3213 public void setPageDescription(
3214 String description, HttpServletRequest request) {
3215
3216 request.setAttribute(WebKeys.PAGE_DESCRIPTION, description);
3217 }
3218
3219
3222 public void setPageKeywords(String keywords, HttpServletRequest request) {
3223 request.removeAttribute(WebKeys.PAGE_KEYWORDS);
3224
3225 addPageKeywords(keywords, request);
3226 }
3227
3228
3231 public void setPageSubtitle(String subtitle, HttpServletRequest request) {
3232 request.setAttribute(WebKeys.PAGE_SUBTITLE, subtitle);
3233 }
3234
3235
3239 public void setPageTitle(String title, HttpServletRequest request) {
3240 request.setAttribute(WebKeys.PAGE_TITLE, title);
3241 }
3242
3243
3246 public void setPortalPort(HttpServletRequest request) {
3247 if (_portalPort.intValue() == -1) {
3248 synchronized (_portalPort) {
3249 _portalPort = new Integer(request.getServerPort());
3250 }
3251 }
3252 }
3253
3254 public void storePreferences(PortletPreferences prefs)
3255 throws IOException, ValidatorException {
3256
3257 PortletPreferencesWrapper prefsWrapper =
3258 (PortletPreferencesWrapper)prefs;
3259
3260 PortletPreferencesImpl prefsImpl = prefsWrapper.getPreferencesImpl();
3261
3262 prefsImpl.store();
3263 }
3264
3265 public String transformCustomSQL(String sql) {
3266 if ((_customSqlClassNames == null) ||
3267 (_customSqlClassNameIds == null)) {
3268
3269 _initCustomSQL();
3270 }
3271
3272 return StringUtil.replace(
3273 sql, _customSqlClassNames, _customSqlClassNameIds);
3274 }
3275
3276 public PortletMode updatePortletMode(
3277 String portletId, User user, Layout layout, PortletMode portletMode,
3278 HttpServletRequest request) {
3279
3280 LayoutTypePortlet layoutType =
3281 (LayoutTypePortlet)layout.getLayoutType();
3282
3283 if (portletMode == null || Validator.isNull(portletMode.toString())) {
3284 if (layoutType.hasModeAboutPortletId(portletId)) {
3285 return LiferayPortletMode.ABOUT;
3286 }
3287 else if (layoutType.hasModeConfigPortletId(portletId)) {
3288 return LiferayPortletMode.CONFIG;
3289 }
3290 else if (layoutType.hasModeEditPortletId(portletId)) {
3291 return PortletMode.EDIT;
3292 }
3293 else if (layoutType.hasModeEditDefaultsPortletId(portletId)) {
3294 return LiferayPortletMode.EDIT_DEFAULTS;
3295 }
3296 else if (layoutType.hasModeEditGuestPortletId(portletId)) {
3297 return LiferayPortletMode.EDIT_GUEST;
3298 }
3299 else if (layoutType.hasModeHelpPortletId(portletId)) {
3300 return PortletMode.HELP;
3301 }
3302 else if (layoutType.hasModePreviewPortletId(portletId)) {
3303 return LiferayPortletMode.PREVIEW;
3304 }
3305 else if (layoutType.hasModePrintPortletId(portletId)) {
3306 return LiferayPortletMode.PRINT;
3307 }
3308 else {
3309 return PortletMode.VIEW;
3310 }
3311 }
3312 else {
3313 boolean updateLayout = false;
3314
3315 if (portletMode.equals(LiferayPortletMode.ABOUT) &&
3316 !layoutType.hasModeAboutPortletId(portletId)) {
3317
3318 layoutType.addModeAboutPortletId(portletId);
3319
3320 updateLayout = true;
3321 }
3322 else if (portletMode.equals(LiferayPortletMode.CONFIG) &&
3323 !layoutType.hasModeConfigPortletId(portletId)) {
3324
3325 layoutType.addModeConfigPortletId(portletId);
3326
3327 updateLayout = true;
3328 }
3329 else if (portletMode.equals(PortletMode.EDIT) &&
3330 !layoutType.hasModeEditPortletId(portletId)) {
3331
3332 layoutType.addModeEditPortletId(portletId);
3333
3334 updateLayout = true;
3335 }
3336 else if (portletMode.equals(LiferayPortletMode.EDIT_DEFAULTS) &&
3337 !layoutType.hasModeEditDefaultsPortletId(portletId)) {
3338
3339 layoutType.addModeEditDefaultsPortletId(portletId);
3340
3341 updateLayout = true;
3342 }
3343 else if (portletMode.equals(LiferayPortletMode.EDIT_GUEST) &&
3344 !layoutType.hasModeEditGuestPortletId(portletId)) {
3345
3346 layoutType.addModeEditGuestPortletId(portletId);
3347
3348 updateLayout = true;
3349 }
3350 else if (portletMode.equals(PortletMode.HELP) &&
3351 !layoutType.hasModeHelpPortletId(portletId)) {
3352
3353 layoutType.addModeHelpPortletId(portletId);
3354
3355 updateLayout = true;
3356 }
3357 else if (portletMode.equals(LiferayPortletMode.PREVIEW) &&
3358 !layoutType.hasModePreviewPortletId(portletId)) {
3359
3360 layoutType.addModePreviewPortletId(portletId);
3361
3362 updateLayout = true;
3363 }
3364 else if (portletMode.equals(LiferayPortletMode.PRINT) &&
3365 !layoutType.hasModePrintPortletId(portletId)) {
3366
3367 layoutType.addModePrintPortletId(portletId);
3368
3369 updateLayout = true;
3370 }
3371 else if (portletMode.equals(PortletMode.VIEW) &&
3372 !layoutType.hasModeViewPortletId(portletId)) {
3373
3374 layoutType.removeModesPortletId(portletId);
3375
3376 updateLayout = true;
3377 }
3378
3379 if (updateLayout) {
3380 LayoutClone layoutClone = LayoutCloneFactory.getInstance();
3381
3382 if (layoutClone != null) {
3383 layoutClone.update(
3384 request, layout.getPlid(), layout.getTypeSettings());
3385 }
3386 }
3387
3388 return portletMode;
3389 }
3390 }
3391
3392 public WindowState updateWindowState(
3393 String portletId, User user, Layout layout, WindowState windowState,
3394 HttpServletRequest request) {
3395
3396 LayoutTypePortlet layoutType =
3397 (LayoutTypePortlet)layout.getLayoutType();
3398
3399 if ((windowState == null) ||
3400 (Validator.isNull(windowState.toString()))) {
3401
3402 if (layoutType.hasStateMaxPortletId(portletId)) {
3403 return WindowState.MAXIMIZED;
3404 }
3405 else if (layoutType.hasStateMinPortletId(portletId)) {
3406 return WindowState.MINIMIZED;
3407 }
3408 else {
3409 return WindowState.NORMAL;
3410 }
3411 }
3412 else {
3413 boolean updateLayout = false;
3414
3415 if (windowState.equals(WindowState.MAXIMIZED) &&
3416 !layoutType.hasStateMaxPortletId(portletId)) {
3417
3418 layoutType.addStateMaxPortletId(portletId);
3419
3420 if (PropsValues.LAYOUT_REMEMBER_MAXIMIZED_WINDOW_STATE) {
3421 updateLayout = true;
3422 }
3423 }
3424 else if (windowState.equals(WindowState.MINIMIZED) &&
3425 !layoutType.hasStateMinPortletId(portletId)) {
3426
3427 layoutType.addStateMinPortletId(portletId);
3428
3429 updateLayout = true;
3430 }
3431 else if (windowState.equals(WindowState.NORMAL) &&
3432 !layoutType.hasStateNormalPortletId(portletId)) {
3433
3434 layoutType.removeStatesPortletId(portletId);
3435
3436 updateLayout = true;
3437 }
3438
3439 if (portletId.equals(PortletKeys.LAYOUT_MANAGEMENT) ||
3440 portletId.equals(PortletKeys.PORTLET_CONFIGURATION)) {
3441
3442 updateLayout = false;
3443 }
3444
3445 if (updateLayout) {
3446 LayoutClone layoutClone = LayoutCloneFactory.getInstance();
3447
3448 if (layoutClone != null) {
3449 layoutClone.update(
3450 request, layout.getPlid(), layout.getTypeSettings());
3451 }
3452 }
3453
3454 return windowState;
3455 }
3456 }
3457
3458 protected long getDoAsUserId(
3459 HttpServletRequest request, String doAsUserIdString,
3460 boolean alwaysAllowDoAsUser)
3461 throws Exception {
3462
3463 if (Validator.isNull(doAsUserIdString)) {
3464 return 0;
3465 }
3466
3467 long doAsUserId = 0;
3468
3469 try {
3470 Company company = getCompany(request);
3471
3472 doAsUserId = GetterUtil.getLong(
3473 Encryptor.decrypt(company.getKeyObj(), doAsUserIdString));
3474 }
3475 catch (Exception e) {
3476 if (_log.isWarnEnabled()) {
3477 _log.warn(
3478 "Unable to impersonate " + doAsUserIdString +
3479 " because the string cannot be decrypted",
3480 e);
3481 }
3482
3483 return 0;
3484 }
3485
3486 if (_log.isDebugEnabled()) {
3487 if (alwaysAllowDoAsUser) {
3488 _log.debug(
3489 "doAsUserId path or Struts action is always allowed");
3490 }
3491 else {
3492 _log.debug(
3493 "doAsUserId path is Struts action not always allowed");
3494 }
3495 }
3496
3497 if (alwaysAllowDoAsUser) {
3498 request.setAttribute(WebKeys.USER_ID, new Long(doAsUserId));
3499
3500 return doAsUserId;
3501 }
3502
3503 HttpSession session = request.getSession();
3504
3505 Long realUserIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
3506
3507 if (realUserIdObj == null) {
3508 return 0;
3509 }
3510
3511 User doAsUser = UserLocalServiceUtil.getUserById(doAsUserId);
3512
3513 long[] organizationIds = doAsUser.getOrganizationIds();
3514
3515 User realUser = UserLocalServiceUtil.getUserById(
3516 realUserIdObj.longValue());
3517 boolean checkGuest = true;
3518
3519 PermissionChecker permissionChecker = null;
3520
3521 try {
3522 permissionChecker = PermissionCheckerFactory.create(
3523 realUser, checkGuest);
3524
3525 if (doAsUser.isDefaultUser() ||
3526 UserPermissionUtil.contains(
3527 permissionChecker, doAsUserId, organizationIds,
3528 ActionKeys.IMPERSONATE)) {
3529
3530 request.setAttribute(WebKeys.USER_ID, new Long(doAsUserId));
3531
3532 return doAsUserId;
3533 }
3534 else {
3535 _log.error(
3536 "User " + realUserIdObj + " does not have the permission " +
3537 "to impersonate " + doAsUserId);
3538
3539 return 0;
3540 }
3541 }
3542 finally {
3543 try {
3544 PermissionCheckerFactory.recycle(permissionChecker);
3545 }
3546 catch (Exception e) {
3547 }
3548 }
3549 }
3550
3551 private long _getPlidFromPortletId(
3552 long groupId, boolean privateLayout, String portletId) {
3553
3554 long plid = LayoutConstants.DEFAULT_PLID;
3555
3556 try {
3557 List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
3558 groupId, privateLayout, LayoutConstants.TYPE_PORTLET);
3559
3560 for (Layout layout : layouts) {
3561 LayoutTypePortlet layoutTypePortlet =
3562 (LayoutTypePortlet)layout.getLayoutType();
3563
3564 if (layoutTypePortlet.hasPortletId(portletId)) {
3565 plid = layout.getPlid();
3566
3567 break;
3568 }
3569 }
3570 }
3571 catch (SystemException se) {
3572 if (_log.isWarnEnabled()) {
3573 _log.warn(se.getMessage(), se);
3574 }
3575 }
3576
3577 return plid;
3578 }
3579
3580 private String _getPortletParam(HttpServletRequest request, String name) {
3581 String value = null;
3582
3583 int valueCount = 0;
3584
3585 Enumeration<String> enu = request.getParameterNames();
3586
3587 while (enu.hasMoreElements()) {
3588 String curName = enu.nextElement();
3589
3590 int pos = curName.indexOf(StringPool.UNDERLINE + name);
3591
3592 if (pos != -1) {
3593 valueCount++;
3594
3595
3597 if (valueCount > 1) {
3598 return StringPool.BLANK;
3599 }
3600
3601 String curValue = ParamUtil.getString(request, curName);
3602
3603 if (Validator.isNotNull(curValue)) {
3604
3605
3607 String portletId1 = curName.substring(1, pos);
3608 String portletId2 = ParamUtil.getString(request, "p_p_id");
3609
3610 if (portletId1.equals(portletId2)) {
3611 value = curValue;
3612 }
3613 }
3614 }
3615 }
3616
3617 if (value == null) {
3618 value = StringPool.BLANK;
3619 }
3620
3621 return value;
3622 }
3623
3624 private String _getServletURL(
3625 Portlet portlet, String servletPath, ThemeDisplay themeDisplay) {
3626
3627 String layoutURL = getLayoutURL(themeDisplay);
3628
3629 Layout layout = themeDisplay.getLayout();
3630
3631 StringBundler sb = new StringBundler();
3632
3633 if (HttpUtil.hasDomain(layoutURL)) {
3634 String protocol = HttpUtil.getProtocol(layoutURL);
3635 String domain = HttpUtil.getDomain(layoutURL);
3636 HttpUtil.removeDomain(layoutURL);
3637
3638 sb.append(protocol);
3639 sb.append(Http.PROTOCOL_DELIMITER);
3640 sb.append(domain);
3641
3642 if (Validator.isNotNull(_pathContext)) {
3643 sb.append(_pathContext);
3644 }
3645
3646 if (themeDisplay.isI18n()) {
3647 sb.append(themeDisplay.getI18nPath());
3648 }
3649
3650 sb.append(servletPath);
3651 sb.append(layout.getFriendlyURL());
3652 }
3653 else {
3654 sb.append(themeDisplay.getPortalURL());
3655
3656 if (Validator.isNotNull(_pathContext)) {
3657 sb.append(_pathContext);
3658 }
3659
3660 if (themeDisplay.isI18n()) {
3661 sb.append(themeDisplay.getI18nPath());
3662 }
3663
3664 sb.append(servletPath);
3665
3666 Group group = layout.getGroup();
3667
3668 if (layout.isPrivateLayout()) {
3669 if (group.isUser()) {
3670 sb.append(_PRIVATE_USER_SERVLET_MAPPING);
3671 }
3672 else {
3673 sb.append(_PRIVATE_GROUP_SERVLET_MAPPING);
3674 }
3675 }
3676 else {
3677 sb.append(_PUBLIC_GROUP_SERVLET_MAPPING);
3678 }
3679
3680 sb.append(group.getFriendlyURL());
3681 sb.append(layout.getFriendlyURL());
3682 }
3683
3684 sb.append(FRIENDLY_URL_SEPARATOR);
3685
3686 FriendlyURLMapper friendlyURLMapper =
3687 portlet.getFriendlyURLMapperInstance();
3688
3689 if ((friendlyURLMapper != null) && !portlet.isInstanceable()) {
3690 sb.append(friendlyURLMapper.getMapping());
3691 }
3692 else {
3693 sb.append(portlet.getPortletId());
3694 }
3695
3696 return sb.toString();
3697 }
3698
3699 private void _initCustomSQL() {
3700 _customSqlClassNames = new String[] {
3701 "[$CLASS_NAME_ID_COM.LIFERAY.PORTAL.MODEL.GROUP$]",
3702 "[$CLASS_NAME_ID_COM.LIFERAY.PORTAL.MODEL.ORGANIZATION$]",
3703 "[$CLASS_NAME_ID_COM.LIFERAY.PORTAL.MODEL.ROLE$]",
3704 "[$CLASS_NAME_ID_COM.LIFERAY.PORTAL.MODEL.USER$]",
3705 "[$CLASS_NAME_ID_COM.LIFERAY.PORTAL.MODEL.USERGROUP$]",
3706 "[$CLASS_NAME_ID_COM.LIFERAY.PORTLET.BLOGS.MODEL.BLOGSENTRY$]",
3707 "[$CLASS_NAME_ID_COM.LIFERAY.PORTLET.BOOKMARKS.MODEL." +
3708 "BOOKMARKSENTRY$]",
3709 "[$CLASS_NAME_ID_COM.LIFERAY.PORTLET.DOCUMENTLIBRARY.MODEL." +
3710 "DLFILEENTRY$]",
3711 "[$CLASS_NAME_ID_COM.LIFERAY.PORTLET.IMAGEGALLERY.MODEL.IGIMAGE$]",
3712 "[$CLASS_NAME_ID_COM.LIFERAY.PORTLET.MESSAGEBOARDS.MODEL." +
3713 "MBMESSAGE$]",
3714 "[$CLASS_NAME_ID_COM.LIFERAY.PORTLET.WIKI.MODEL.WIKIPAGE$]",
3715 "[$FALSE$]",
3716 "[$TRUE$]"
3717 };
3718
3719 DB db = DBFactoryUtil.getDB();
3720
3721 _customSqlClassNameIds = new String[] {
3722 String.valueOf(PortalUtil.getClassNameId(Group.class)),
3723 String.valueOf(PortalUtil.getClassNameId(Organization.class)),
3724 String.valueOf(PortalUtil.getClassNameId(Role.class)),
3725 String.valueOf(PortalUtil.getClassNameId(User.class)),
3726 String.valueOf(PortalUtil.getClassNameId(UserGroup.class)),
3727 String.valueOf(PortalUtil.getClassNameId(BlogsEntry.class)),
3728 String.valueOf(PortalUtil.getClassNameId(BookmarksEntry.class)),
3729 String.valueOf(PortalUtil.getClassNameId(DLFileEntry.class)),
3730 String.valueOf(PortalUtil.getClassNameId(IGImage.class)),
3731 String.valueOf(PortalUtil.getClassNameId(MBMessage.class)),
3732 String.valueOf(PortalUtil.getClassNameId(WikiPage.class)),
3733 db.getTemplateFalse(),
3734 db.getTemplateTrue()
3735 };
3736 }
3737
3738 private static final String _J_SECURITY_CHECK = "j_security_check";
3739
3740 private static final String _JSESSIONID = ";jsessionid=";
3741
3742 private static final String _LOCALHOST = "localhost";
3743
3744 private static final String _PRIVATE_GROUP_SERVLET_MAPPING =
3745 PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING;
3746
3747 private static final String _PRIVATE_USER_SERVLET_MAPPING =
3748 PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING;
3749
3750 private static final String _PUBLIC_GROUP_SERVLET_MAPPING =
3751 PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING;
3752
3753 private static final Date _UP_TIME = new Date();
3754
3755 private static Log _log = LogFactoryUtil.getLog(PortalImpl.class);
3756
3757 private String[] _allSystemCommunityRoles;
3758 private String[] _allSystemGroups;
3759 private String[] _allSystemOrganizationRoles;
3760 private String[] _allSystemRoles;
3761 private String _cdnHostHttp;
3762 private String _cdnHostHttps;
3763 private String _computerAddress;
3764 private String _computerName;
3765 private String[] _customSqlClassNameIds;
3766 private String[] _customSqlClassNames;
3767 private String _globalLibDir;
3768 private String _pathContext;
3769 private String _pathFriendlyURLPrivateGroup;
3770 private String _pathFriendlyURLPrivateUser;
3771 private String _pathFriendlyURLPublic;
3772 private String _pathImage;
3773 private String _pathMain;
3774 private Map<String, Long> _plidToPortletIdCache =
3775 new ConcurrentHashMap<String, Long>();
3776 private String _portalLibDir;
3777 private Integer _portalPort = new Integer(-1);
3778 private String _portalWebDir;
3779 private Set<String> _reservedParams;
3780 private String[] _sortedSystemCommunityRoles;
3781 private String[] _sortedSystemGroups;
3782 private String[] _sortedSystemOrganizationRoles;
3783 private String[] _sortedSystemRoles;
3784
3785}