001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
019    import com.liferay.portal.kernel.cache.key.CacheKeyGenerator;
020    import com.liferay.portal.kernel.cache.key.CacheKeyGeneratorUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.model.Group;
027    import com.liferay.portal.kernel.model.Layout;
028    import com.liferay.portal.kernel.model.LayoutConstants;
029    import com.liferay.portal.kernel.model.LayoutTypePortlet;
030    import com.liferay.portal.kernel.model.Portlet;
031    import com.liferay.portal.kernel.model.PortletConstants;
032    import com.liferay.portal.kernel.model.PortletPreferencesIds;
033    import com.liferay.portal.kernel.portlet.LiferayPortletMode;
034    import com.liferay.portal.kernel.portlet.PortalPreferences;
035    import com.liferay.portal.kernel.portlet.PortletPreferencesFactory;
036    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryConstants;
037    import com.liferay.portal.kernel.security.auth.PrincipalException;
038    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
039    import com.liferay.portal.kernel.security.permission.ActionKeys;
040    import com.liferay.portal.kernel.security.permission.PermissionChecker;
041    import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
042    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
043    import com.liferay.portal.kernel.service.PortalPreferencesLocalServiceUtil;
044    import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
045    import com.liferay.portal.kernel.service.PortletPreferencesLocalServiceUtil;
046    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
047    import com.liferay.portal.kernel.service.permission.LayoutPermissionUtil;
048    import com.liferay.portal.kernel.service.permission.PortletPermissionUtil;
049    import com.liferay.portal.kernel.theme.ThemeDisplay;
050    import com.liferay.portal.kernel.util.GetterUtil;
051    import com.liferay.portal.kernel.util.JavaConstants;
052    import com.liferay.portal.kernel.util.ParamUtil;
053    import com.liferay.portal.kernel.util.PortalUtil;
054    import com.liferay.portal.kernel.util.PortletKeys;
055    import com.liferay.portal.kernel.util.StringPool;
056    import com.liferay.portal.kernel.util.Validator;
057    import com.liferay.portal.kernel.util.WebKeys;
058    import com.liferay.portal.util.PropsValues;
059    import com.liferay.portal.xml.StAXReaderUtil;
060    import com.liferay.portlet.portletconfiguration.util.ConfigurationPortletRequest;
061    
062    import java.util.ArrayList;
063    import java.util.Collections;
064    import java.util.HashMap;
065    import java.util.List;
066    import java.util.Map;
067    
068    import javax.portlet.PortletPreferences;
069    import javax.portlet.PortletRequest;
070    import javax.portlet.PreferencesValidator;
071    import javax.portlet.filter.PortletRequestWrapper;
072    
073    import javax.servlet.http.HttpServletRequest;
074    import javax.servlet.http.HttpSession;
075    
076    import javax.xml.stream.XMLEventReader;
077    import javax.xml.stream.XMLInputFactory;
078    import javax.xml.stream.XMLStreamException;
079    import javax.xml.stream.events.EndElement;
080    import javax.xml.stream.events.StartElement;
081    import javax.xml.stream.events.XMLEvent;
082    
083    /**
084     * @author Brian Wing Shun Chan
085     * @author Alexander Chow
086     * @author Minhchau Dang
087     * @author Raymond Augé
088     */
089    @DoPrivileged
090    public class PortletPreferencesFactoryImpl
091            implements PortletPreferencesFactory {
092    
093            public static Map<String, Preference> createPreferencesMap(String xml) {
094                    XMLEventReader xmlEventReader = null;
095    
096                    Map<String, Preference> preferencesMap = new HashMap<>();
097    
098                    try {
099                            XMLInputFactory xmlInputFactory =
100                                    StAXReaderUtil.getXMLInputFactory();
101    
102                            xmlEventReader = xmlInputFactory.createXMLEventReader(
103                                    new UnsyncStringReader(xml));
104    
105                            while (xmlEventReader.hasNext()) {
106                                    XMLEvent xmlEvent = xmlEventReader.nextEvent();
107    
108                                    if (xmlEvent.isStartElement()) {
109                                            StartElement startElement = xmlEvent.asStartElement();
110    
111                                            String elementName = startElement.getName().getLocalPart();
112    
113                                            if (elementName.equals("preference")) {
114                                                    Preference preference = readPreference(xmlEventReader);
115    
116                                                    preferencesMap.put(preference.getName(), preference);
117                                            }
118                                    }
119                            }
120                    }
121                    catch (XMLStreamException xmlse) {
122                            throw new SystemException(xmlse);
123                    }
124                    finally {
125                            if (xmlEventReader != null) {
126                                    try {
127                                            xmlEventReader.close();
128                                    }
129                                    catch (XMLStreamException xmlse) {
130                                            if (_log.isDebugEnabled()) {
131                                                    _log.debug(xmlse, xmlse);
132                                            }
133                                    }
134                            }
135                    }
136    
137                    return preferencesMap;
138            }
139    
140            @Override
141            public void checkControlPanelPortletPreferences(
142                            ThemeDisplay themeDisplay, Portlet portlet)
143                    throws PortalException {
144    
145                    Layout layout = themeDisplay.getLayout();
146    
147                    Group group = layout.getGroup();
148    
149                    if (!group.isControlPanel()) {
150                            return;
151                    }
152    
153                    String portletId = portlet.getPortletId();
154    
155                    boolean hasControlPanelAccessPermission =
156                            PortletPermissionUtil.hasControlPanelAccessPermission(
157                                    themeDisplay.getPermissionChecker(),
158                                    themeDisplay.getScopeGroupId(), portletId);
159    
160                    if (!hasControlPanelAccessPermission) {
161                            return;
162                    }
163    
164                    PortletPreferences portletSetup = getStrictLayoutPortletSetup(
165                            layout, portletId);
166    
167                    if (portletSetup instanceof StrictPortletPreferencesImpl) {
168                            getLayoutPortletSetup(layout, portletId);
169                    }
170    
171                    if (portlet.isInstanceable()) {
172                            return;
173                    }
174    
175                    PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
176                            themeDisplay.getScopeGroupId(), themeDisplay.getUserId(), layout,
177                            portletId, false);
178    
179                    PortletPreferencesLocalServiceUtil.getPreferences(
180                            portletPreferencesIds);
181            }
182    
183            @Override
184            public PortletPreferences fromDefaultXML(String xml) {
185                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
186    
187                    return new PortletPreferencesImpl(xml, preferencesMap);
188            }
189    
190            @Override
191            public PortalPreferencesImpl fromXML(
192                    long ownerId, int ownerType, String xml) {
193    
194                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
195    
196                    return new PortalPreferencesImpl(
197                            ownerId, ownerType, xml, preferencesMap, false);
198            }
199    
200            @Override
201            public PortletPreferencesImpl fromXML(
202                    long companyId, long ownerId, int ownerType, long plid,
203                    String portletId, String xml) {
204    
205                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
206    
207                    return new PortletPreferencesImpl(
208                            companyId, ownerId, ownerType, plid, portletId, xml,
209                            preferencesMap);
210            }
211    
212            /**
213             * @deprecated As of 6.2.0, replaced by {@link #fromXML(long, int, String)}
214             */
215            @Deprecated
216            @Override
217            public PortalPreferences fromXML(
218                    long companyId, long ownerId, int ownerType, String xml) {
219    
220                    return fromXML(ownerId, ownerType, xml);
221            }
222    
223            @Override
224            public PortletPreferences getExistingPortletSetup(
225                            Layout layout, String portletId)
226                    throws PortalException {
227    
228                    if (Validator.isNull(portletId)) {
229                            return null;
230                    }
231    
232                    PortletPreferences portletPreferences = getStrictPortletSetup(
233                            layout, portletId);
234    
235                    if (portletPreferences instanceof StrictPortletPreferencesImpl) {
236                            throw new PrincipalException();
237                    }
238    
239                    return portletPreferences;
240            }
241    
242            @Override
243            public PortletPreferences getExistingPortletSetup(
244                            PortletRequest portletRequest)
245                    throws PortalException {
246    
247                    String portletResource = ParamUtil.getString(
248                            portletRequest, "portletResource");
249    
250                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
251                            WebKeys.THEME_DISPLAY);
252    
253                    return getExistingPortletSetup(
254                            themeDisplay.getLayout(), portletResource);
255            }
256    
257            @Override
258            public PortletPreferences getLayoutPortletSetup(
259                    Layout layout, String portletId) {
260    
261                    return getLayoutPortletSetup(layout, portletId, null);
262            }
263    
264            @Override
265            public PortletPreferences getLayoutPortletSetup(
266                    Layout layout, String portletId, String defaultPreferences) {
267    
268                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
269                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
270    
271                    if (PortletConstants.hasUserId(portletId)) {
272                            ownerId = PortletConstants.getUserId(portletId);
273                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
274                    }
275    
276                    return getLayoutPortletSetup(
277                            layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
278                            portletId, defaultPreferences);
279            }
280    
281            @Override
282            public PortletPreferences getLayoutPortletSetup(
283                    long companyId, long ownerId, int ownerType, long plid,
284                    String portletId, String defaultPreferences) {
285    
286                    return PortletPreferencesLocalServiceUtil.getPreferences(
287                            companyId, ownerId, ownerType, plid, portletId, defaultPreferences);
288            }
289    
290            @Override
291            public PortalPreferences getPortalPreferences(HttpServletRequest request) {
292                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
293                            WebKeys.THEME_DISPLAY);
294    
295                    return getPortalPreferences(
296                            request.getSession(), themeDisplay.getUserId(),
297                            themeDisplay.isSignedIn());
298            }
299    
300            @Override
301            public PortalPreferences getPortalPreferences(
302                    HttpSession session, long userId, boolean signedIn) {
303    
304                    long ownerId = userId;
305                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
306    
307                    PortalPreferences portalPreferences = null;
308    
309                    if (signedIn) {
310                            PortalPreferencesWrapper portalPreferencesWrapper =
311                                    PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
312    
313                            if (portalPreferencesWrapper == null) {
314                                    portalPreferencesWrapper =
315                                            (PortalPreferencesWrapper)
316                                                    PortalPreferencesLocalServiceUtil.getPreferences(
317                                                            ownerId, ownerType);
318    
319                                    portalPreferences =
320                                            portalPreferencesWrapper.getPortalPreferencesImpl();
321                            }
322                            else {
323                                    PortalPreferencesImpl portalPreferencesImpl =
324                                            portalPreferencesWrapper.getPortalPreferencesImpl();
325    
326                                    portalPreferences = portalPreferencesImpl.clone();
327                            }
328                    }
329                    else {
330                            if (session != null) {
331                                    portalPreferences = (PortalPreferences)session.getAttribute(
332                                            WebKeys.PORTAL_PREFERENCES);
333                            }
334    
335                            if (portalPreferences == null) {
336                                    PortalPreferencesWrapper portalPreferencesWrapper =
337                                            PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
338    
339                                    if (portalPreferencesWrapper == null) {
340                                            portalPreferencesWrapper =
341                                                    (PortalPreferencesWrapper)
342                                                            PortalPreferencesLocalServiceUtil.getPreferences(
343                                                                    ownerId, ownerType);
344    
345                                            portalPreferences =
346                                                    portalPreferencesWrapper.getPortalPreferencesImpl();
347                                    }
348                                    else {
349                                            PortalPreferencesImpl portalPreferencesImpl =
350                                                    portalPreferencesWrapper.getPortalPreferencesImpl();
351    
352                                            portalPreferences = portalPreferencesImpl.clone();
353                                    }
354    
355                                    if (session != null) {
356                                            session.setAttribute(
357                                                    WebKeys.PORTAL_PREFERENCES, portalPreferences);
358                                    }
359                            }
360                    }
361    
362                    portalPreferences.setSignedIn(signedIn);
363                    portalPreferences.setUserId(userId);
364    
365                    return portalPreferences;
366            }
367    
368            /**
369             * @deprecated As of 6.2.0, replaced by {@link
370             *             #getPortalPreferences(HttpSession, long, boolean)}
371             */
372            @Deprecated
373            @Override
374            public PortalPreferences getPortalPreferences(
375                    HttpSession session, long companyId, long userId, boolean signedIn) {
376    
377                    return getPortalPreferences(session, userId, signedIn);
378            }
379    
380            @Override
381            public PortalPreferences getPortalPreferences(
382                    long userId, boolean signedIn) {
383    
384                    return getPortalPreferences(null, userId, signedIn);
385            }
386    
387            /**
388             * @deprecated As of 6.2.0, replaced by {@link #getPortalPreferences(long,
389             *             boolean)}
390             */
391            @Deprecated
392            @Override
393            public PortalPreferences getPortalPreferences(
394                    long companyId, long userId, boolean signedIn) {
395    
396                    return getPortalPreferences(userId, signedIn);
397            }
398    
399            @Override
400            public PortalPreferences getPortalPreferences(
401                    PortletRequest portletRequest) {
402    
403                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
404                            portletRequest);
405    
406                    return getPortalPreferences(request);
407            }
408    
409            @Override
410            public PortletPreferences getPortletPreferences(
411                            HttpServletRequest request, String portletId)
412                    throws PortalException {
413    
414                    PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
415                            request, portletId);
416    
417                    return PortletPreferencesLocalServiceUtil.getPreferences(
418                            portletPreferencesIds);
419            }
420    
421            @Override
422            public PortletPreferencesIds getPortletPreferencesIds(
423                            HttpServletRequest request, Layout layout, String portletId)
424                    throws PortalException {
425    
426                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
427                            WebKeys.THEME_DISPLAY);
428    
429                    long siteGroupId = themeDisplay.getSiteGroupId();
430                    long userId = PortalUtil.getUserId(request);
431                    LayoutTypePortlet layoutTypePortlet =
432                            themeDisplay.getLayoutTypePortlet();
433    
434                    boolean modeEditGuest = false;
435    
436                    String portletMode = ParamUtil.getString(request, "p_p_mode");
437    
438                    if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString()) ||
439                            ((layoutTypePortlet != null) &&
440                             layoutTypePortlet.hasModeEditGuestPortletId(portletId))) {
441    
442                            modeEditGuest = true;
443                    }
444    
445                    return getPortletPreferencesIds(
446                            siteGroupId, userId, layout, portletId, modeEditGuest);
447            }
448    
449            @Override
450            public PortletPreferencesIds getPortletPreferencesIds(
451                            HttpServletRequest request, String portletId)
452                    throws PortalException {
453    
454                    Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
455    
456                    return getPortletPreferencesIds(request, layout, portletId);
457            }
458    
459            @Override
460            public PortletPreferencesIds getPortletPreferencesIds(
461                            long siteGroupId, long userId, Layout layout, String portletId,
462                            boolean modeEditGuest)
463                    throws PortalException {
464    
465                    PermissionChecker permissionChecker =
466                            PermissionThreadLocal.getPermissionChecker();
467    
468                    String originalPortletId = portletId;
469    
470                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
471                            layout.getCompanyId(), portletId);
472    
473                    long ownerId = 0;
474                    int ownerType = 0;
475                    long plid = 0;
476    
477                    if (modeEditGuest) {
478                            boolean hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
479                                    permissionChecker, layout, ActionKeys.UPDATE);
480    
481                            if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
482                            }
483                            else {
484    
485                                    // Only users with the correct permissions can update guest
486                                    // preferences
487    
488                                    throw new PrincipalException.MustHavePermission(
489                                            permissionChecker, Layout.class.getName(),
490                                            layout.getLayoutId(), ActionKeys.UPDATE);
491                            }
492                    }
493    
494                    if (PortletConstants.hasUserId(originalPortletId) &&
495                            (PortletConstants.getUserId(originalPortletId) == userId)) {
496    
497                            ownerId = userId;
498                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
499                            plid = layout.getPlid();
500                    }
501                    else if (portlet.isPreferencesCompanyWide()) {
502                            ownerId = layout.getCompanyId();
503                            ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
504                            plid = PortletKeys.PREFS_PLID_SHARED;
505                            portletId = PortletConstants.getRootPortletId(portletId);
506                    }
507                    else {
508                            if (portlet.isPreferencesUniquePerLayout()) {
509                                    ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
510                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
511                                    plid = layout.getPlid();
512    
513                                    LayoutTypePortlet layoutTypePortlet =
514                                            (LayoutTypePortlet)layout.getLayoutType();
515    
516                                    if (layoutTypePortlet.isPortletEmbedded(portletId)) {
517                                            ownerId = layout.getGroupId();
518                                            plid = PortletKeys.PREFS_PLID_SHARED;
519                                    }
520    
521                                    if (portlet.isPreferencesOwnedByGroup()) {
522                                    }
523                                    else {
524                                            if ((userId <= 0) || modeEditGuest) {
525                                                    userId = UserLocalServiceUtil.getDefaultUserId(
526                                                            layout.getCompanyId());
527                                            }
528    
529                                            ownerId = userId;
530                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
531                                    }
532                            }
533                            else {
534                                    plid = PortletKeys.PREFS_PLID_SHARED;
535    
536                                    if (portlet.isPreferencesOwnedByGroup()) {
537                                            ownerId = siteGroupId;
538                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
539                                            portletId = PortletConstants.getRootPortletId(portletId);
540                                    }
541                                    else {
542                                            if ((userId <= 0) || modeEditGuest) {
543                                                    userId = UserLocalServiceUtil.getDefaultUserId(
544                                                            layout.getCompanyId());
545                                            }
546    
547                                            ownerId = userId;
548                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
549                                    }
550                            }
551                    }
552    
553                    return new PortletPreferencesIds(
554                            layout.getCompanyId(), ownerId, ownerType, plid, portletId);
555            }
556    
557            @Override
558            public PortletPreferencesIds getPortletPreferencesIds(
559                    long companyId, long siteGroupId, long plid, String portletId,
560                    String settingsScope) {
561    
562                    int ownerType = 0;
563                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
564    
565                    if (settingsScope.equals(
566                                    PortletPreferencesFactoryConstants.SETTINGS_SCOPE_COMPANY)) {
567    
568                            ownerId = companyId;
569                            ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
570                            plid = PortletKeys.PREFS_PLID_SHARED;
571                    }
572                    else if (settingsScope.equals(
573                                            PortletPreferencesFactoryConstants.SETTINGS_SCOPE_GROUP)) {
574    
575                            ownerId = siteGroupId;
576                            ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
577                            plid = PortletKeys.PREFS_PLID_SHARED;
578                    }
579                    else if (settingsScope.equals(
580                                            PortletPreferencesFactoryConstants.
581                                                    SETTINGS_SCOPE_PORTLET_INSTANCE)) {
582    
583                            ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
584                            ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
585    
586                            if (PortletConstants.hasUserId(portletId)) {
587                                    ownerId = PortletConstants.getUserId(portletId);
588                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
589                            }
590                    }
591    
592                    return new PortletPreferencesIds(
593                            companyId, ownerId, ownerType, plid, portletId);
594            }
595    
596            @Override
597            public PortletPreferences getPortletSetup(
598                    HttpServletRequest request, String portletId) {
599    
600                    return getPortletSetup(request, portletId, null);
601            }
602    
603            @Override
604            public PortletPreferences getPortletSetup(
605                    HttpServletRequest request, String portletId,
606                    String defaultPreferences) {
607    
608                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
609                            JavaConstants.JAVAX_PORTLET_REQUEST);
610    
611                    if (portletRequest instanceof ConfigurationPortletRequest) {
612                            PortletRequestWrapper portletRequestWrapper =
613                                    (PortletRequestWrapper)portletRequest;
614    
615                            return portletRequestWrapper.getPreferences();
616                    }
617    
618                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
619                            WebKeys.THEME_DISPLAY);
620    
621                    return getPortletSetup(
622                            themeDisplay.getSiteGroupId(), themeDisplay.getLayout(), portletId,
623                            defaultPreferences);
624            }
625    
626            @Override
627            public PortletPreferences getPortletSetup(
628                    Layout layout, String portletId, String defaultPreferences) {
629    
630                    return getPortletSetup(
631                            LayoutConstants.DEFAULT_PLID, layout, portletId,
632                            defaultPreferences);
633            }
634    
635            @Override
636            public PortletPreferences getPortletSetup(
637                    long siteGroupId, Layout layout, String portletId,
638                    String defaultPreferences) {
639    
640                    return getPortletSetup(
641                            layout.getCompanyId(), siteGroupId, layout.getGroupId(),
642                            layout.getPlid(), portletId, defaultPreferences, false);
643            }
644    
645            @Override
646            public PortletPreferences getPortletSetup(PortletRequest portletRequest) {
647                    String portletId = PortalUtil.getPortletId(portletRequest);
648    
649                    return getPortletSetup(portletRequest, portletId);
650            }
651    
652            @Override
653            public PortletPreferences getPortletSetup(
654                    PortletRequest portletRequest, String portletId) {
655    
656                    if (portletRequest instanceof ConfigurationPortletRequest) {
657                            PortletRequestWrapper portletRequestWrapper =
658                                    (PortletRequestWrapper)portletRequest;
659    
660                            return portletRequestWrapper.getPreferences();
661                    }
662    
663                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
664                            portletRequest);
665    
666                    return getPortletSetup(request, portletId);
667            }
668    
669            @Override
670            public Map<Long, PortletPreferences> getPortletSetupMap(
671                    long companyId, long groupId, long ownerId, int ownerType,
672                    String portletId, boolean privateLayout) {
673    
674                    Map<Long, PortletPreferences> portletSetupMap = new HashMap<>();
675    
676                    List<com.liferay.portal.kernel.model.PortletPreferences>
677                            portletPreferencesList =
678                                    PortletPreferencesLocalServiceUtil.getPortletPreferences(
679                                            companyId, groupId, ownerId, ownerType, portletId,
680                                            privateLayout);
681    
682                    for (com.liferay.portal.kernel.model.PortletPreferences
683                                    portletPreferences : portletPreferencesList) {
684    
685                            PortletPreferences portletSetup =
686                                    PortletPreferencesLocalServiceUtil.getPreferences(
687                                            companyId, ownerId, ownerType, portletPreferences.getPlid(),
688                                            portletId);
689    
690                            portletSetupMap.put(portletPreferences.getPlid(), portletSetup);
691                    }
692    
693                    return portletSetupMap;
694            }
695    
696            @Override
697            public PortletPreferences getPreferences(HttpServletRequest request) {
698                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
699                            JavaConstants.JAVAX_PORTLET_REQUEST);
700    
701                    PortletPreferences portletPreferences = null;
702    
703                    if (portletRequest != null) {
704                            PortletPreferencesWrapper portletPreferencesWrapper =
705                                    (PortletPreferencesWrapper)portletRequest.getPreferences();
706    
707                            portletPreferences =
708                                    portletPreferencesWrapper.getPortletPreferencesImpl();
709                    }
710    
711                    return portletPreferences;
712            }
713    
714            @Override
715            public PreferencesValidator getPreferencesValidator(Portlet portlet) {
716                    return PortalUtil.getPreferencesValidator(portlet);
717            }
718    
719            @Override
720            public PortletPreferences getStrictLayoutPortletSetup(
721                    Layout layout, String portletId) {
722    
723                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
724                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
725    
726                    if (PortletConstants.hasUserId(portletId)) {
727                            ownerId = PortletConstants.getUserId(portletId);
728                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
729                    }
730    
731                    return PortletPreferencesLocalServiceUtil.getStrictPreferences(
732                            layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
733                            portletId);
734            }
735    
736            @Override
737            public PortletPreferences getStrictPortletSetup(
738                    Layout layout, String portletId) {
739    
740                    return getPortletSetup(
741                            layout.getCompanyId(), LayoutConstants.DEFAULT_PLID,
742                            layout.getGroupId(), layout.getPlid(), portletId, StringPool.BLANK,
743                            true);
744            }
745    
746            @Override
747            public PortletPreferences getStrictPortletSetup(
748                    long companyId, long groupId, String portletId) {
749    
750                    return getPortletSetup(
751                            companyId, LayoutConstants.DEFAULT_PLID, groupId,
752                            LayoutConstants.DEFAULT_PLID, portletId, StringPool.BLANK, true);
753            }
754    
755            @Override
756            public StrictPortletPreferencesImpl strictFromXML(
757                    long companyId, long ownerId, int ownerType, long plid,
758                    String portletId, String xml) {
759    
760                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
761    
762                    return new StrictPortletPreferencesImpl(
763                            companyId, ownerId, ownerType, plid, portletId, xml,
764                            preferencesMap);
765            }
766    
767            @Override
768            public String toXML(PortalPreferences portalPreferences) {
769                    PortalPreferencesImpl portalPreferencesImpl = null;
770    
771                    if (portalPreferences instanceof PortalPreferencesWrapper) {
772                            PortalPreferencesWrapper portalPreferencesWrapper =
773                                    (PortalPreferencesWrapper)portalPreferences;
774    
775                            portalPreferencesImpl =
776                                    portalPreferencesWrapper.getPortalPreferencesImpl();
777                    }
778                    else {
779                            portalPreferencesImpl = (PortalPreferencesImpl)portalPreferences;
780                    }
781    
782                    return portalPreferencesImpl.toXML();
783            }
784    
785            @Override
786            public String toXML(PortletPreferences portletPreferences) {
787                    PortletPreferencesImpl portletPreferencesImpl = null;
788    
789                    if (portletPreferences instanceof PortletPreferencesWrapper) {
790                            PortletPreferencesWrapper portletPreferencesWrapper =
791                                    (PortletPreferencesWrapper)portletPreferences;
792    
793                            portletPreferencesImpl =
794                                    portletPreferencesWrapper.getPortletPreferencesImpl();
795                    }
796                    else {
797                            portletPreferencesImpl = (PortletPreferencesImpl)portletPreferences;
798                    }
799    
800                    return portletPreferencesImpl.toXML();
801            }
802    
803            protected static Preference readPreference(XMLEventReader xmlEventReader)
804                    throws XMLStreamException {
805    
806                    String name = null;
807                    List<String> values = new ArrayList<>();
808                    boolean readOnly = false;
809    
810                    while (xmlEventReader.hasNext()) {
811                            XMLEvent xmlEvent = xmlEventReader.nextEvent();
812    
813                            if (xmlEvent.isStartElement()) {
814                                    StartElement startElement = xmlEvent.asStartElement();
815    
816                                    String elementName = startElement.getName().getLocalPart();
817    
818                                    if (elementName.equals("name")) {
819                                            name = StAXReaderUtil.read(xmlEventReader);
820                                    }
821                                    else if (elementName.equals("value")) {
822                                            String value = StAXReaderUtil.read(xmlEventReader);
823    
824                                            values.add(value);
825                                    }
826                                    else if (elementName.equals("read-only")) {
827                                            String value = StAXReaderUtil.read(xmlEventReader);
828    
829                                            readOnly = GetterUtil.getBoolean(value);
830                                    }
831                            }
832                            else if (xmlEvent.isEndElement()) {
833                                    EndElement endElement = xmlEvent.asEndElement();
834    
835                                    String elementName = endElement.getName().getLocalPart();
836    
837                                    if (elementName.equals("preference")) {
838                                            break;
839                                    }
840                            }
841                    }
842    
843                    return new Preference(
844                            name, values.toArray(new String[values.size()]), readOnly);
845            }
846    
847            protected PortletPreferences getPortletSetup(
848                    long companyId, long siteGroupId, long layoutGroupId, long plid,
849                    String portletId, String defaultPreferences, boolean strictMode) {
850    
851                    String originalPortletId = portletId;
852    
853                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
854                            companyId, portletId);
855    
856                    boolean uniquePerLayout = false;
857                    boolean uniquePerGroup = false;
858    
859                    if (portlet.isPreferencesCompanyWide()) {
860                            portletId = PortletConstants.getRootPortletId(portletId);
861                    }
862                    else {
863                            if (portlet.isPreferencesUniquePerLayout()) {
864                                    uniquePerLayout = true;
865    
866                                    if (portlet.isPreferencesOwnedByGroup()) {
867                                            uniquePerGroup = true;
868                                    }
869                            }
870                            else {
871                                    if (portlet.isPreferencesOwnedByGroup()) {
872                                            uniquePerGroup = true;
873                                            portletId = PortletConstants.getRootPortletId(portletId);
874                                    }
875                            }
876                    }
877    
878                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
879                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
880    
881                    Group group = GroupLocalServiceUtil.fetchGroup(siteGroupId);
882    
883                    if ((group != null) && group.isLayout()) {
884                            plid = group.getClassPK();
885                    }
886    
887                    if (PortletConstants.hasUserId(originalPortletId)) {
888                            ownerId = PortletConstants.getUserId(originalPortletId);
889                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
890                    }
891                    else if (!uniquePerLayout) {
892                            plid = PortletKeys.PREFS_PLID_SHARED;
893    
894                            if (uniquePerGroup) {
895                                    if (siteGroupId > LayoutConstants.DEFAULT_PLID) {
896                                            ownerId = siteGroupId;
897                                    }
898                                    else {
899                                            ownerId = layoutGroupId;
900                                    }
901    
902                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
903                            }
904                            else {
905                                    ownerId = companyId;
906                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
907                            }
908                    }
909    
910                    if (strictMode) {
911                            return PortletPreferencesLocalServiceUtil.getStrictPreferences(
912                                    companyId, ownerId, ownerType, plid, portletId);
913                    }
914    
915                    return PortletPreferencesLocalServiceUtil.getPreferences(
916                            companyId, ownerId, ownerType, plid, portletId, defaultPreferences);
917            }
918    
919            protected Map<String, Preference> toPreferencesMap(String xml) {
920                    if (Validator.isNull(xml)) {
921                            return Collections.emptyMap();
922                    }
923    
924                    String cacheKey = _encodeCacheKey(xml);
925    
926                    Map<String, Preference> preferencesMap = _preferencesMapPortalCache.get(
927                            cacheKey);
928    
929                    if (preferencesMap != null) {
930                            return preferencesMap;
931                    }
932    
933                    preferencesMap = createPreferencesMap(xml);
934    
935                    _preferencesMapPortalCache.put(cacheKey, preferencesMap);
936    
937                    return preferencesMap;
938            }
939    
940            private String _encodeCacheKey(String xml) {
941                    if (xml.length() <=
942                                    PropsValues.PORTLET_PREFERENCES_CACHE_KEY_THRESHOLD_SIZE) {
943    
944                            return xml;
945                    }
946    
947                    CacheKeyGenerator cacheKeyGenerator =
948                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
949                                    PortletPreferencesFactoryImpl.class.getName());
950    
951                    if (_log.isDebugEnabled()) {
952                            _log.debug("Cache key generator " + cacheKeyGenerator.getClass());
953                    }
954    
955                    return String.valueOf(cacheKeyGenerator.getCacheKey(xml));
956            }
957    
958            private static final Log _log = LogFactoryUtil.getLog(
959                    PortletPreferencesFactoryImpl.class);
960    
961            private final PortalCache<String, Map<String, Preference>>
962                    _preferencesMapPortalCache = SingleVMPoolUtil.getPortalCache(
963                            PortletPreferencesFactoryImpl.class.getName());
964    
965    }