001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020 import com.liferay.portal.kernel.portlet.LiferayPortletMode;
021 import com.liferay.portal.kernel.security.pacl.DoPrivileged;
022 import com.liferay.portal.kernel.util.GetterUtil;
023 import com.liferay.portal.kernel.util.JavaConstants;
024 import com.liferay.portal.kernel.util.ParamUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.model.Group;
028 import com.liferay.portal.model.Layout;
029 import com.liferay.portal.model.LayoutConstants;
030 import com.liferay.portal.model.LayoutTypePortlet;
031 import com.liferay.portal.model.Portlet;
032 import com.liferay.portal.model.PortletConstants;
033 import com.liferay.portal.model.PortletPreferencesIds;
034 import com.liferay.portal.security.auth.PrincipalException;
035 import com.liferay.portal.security.permission.ActionKeys;
036 import com.liferay.portal.security.permission.PermissionChecker;
037 import com.liferay.portal.security.permission.PermissionThreadLocal;
038 import com.liferay.portal.service.GroupLocalServiceUtil;
039 import com.liferay.portal.service.PortalPreferencesLocalServiceUtil;
040 import com.liferay.portal.service.PortletLocalServiceUtil;
041 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
042 import com.liferay.portal.service.UserLocalServiceUtil;
043 import com.liferay.portal.service.permission.LayoutPermissionUtil;
044 import com.liferay.portal.theme.ThemeDisplay;
045 import com.liferay.portal.util.PortalUtil;
046 import com.liferay.portal.util.PortletKeys;
047 import com.liferay.portal.util.WebKeys;
048 import com.liferay.portal.xml.StAXReaderUtil;
049 import com.liferay.portlet.portletconfiguration.util.ConfigurationPortletRequest;
050
051 import java.util.ArrayList;
052 import java.util.HashMap;
053 import java.util.List;
054 import java.util.Map;
055
056 import javax.portlet.PortletPreferences;
057 import javax.portlet.PortletRequest;
058 import javax.portlet.PreferencesValidator;
059 import javax.portlet.filter.PortletRequestWrapper;
060
061 import javax.servlet.http.HttpServletRequest;
062 import javax.servlet.http.HttpSession;
063
064 import javax.xml.stream.XMLEventReader;
065 import javax.xml.stream.XMLInputFactory;
066 import javax.xml.stream.XMLStreamException;
067 import javax.xml.stream.events.EndElement;
068 import javax.xml.stream.events.StartElement;
069 import javax.xml.stream.events.XMLEvent;
070
071
077 @DoPrivileged
078 public class PortletPreferencesFactoryImpl
079 implements PortletPreferencesFactory {
080
081 @Override
082 public PortletPreferences fromDefaultXML(String xml)
083 throws SystemException {
084
085 Map<String, Preference> preferencesMap =
086 new HashMap<String, Preference>();
087
088 populateMap(xml, preferencesMap);
089
090 return new PortletPreferencesImpl(xml, preferencesMap);
091 }
092
093 @Override
094 public PortletPreferencesImpl fromXML(
095 long companyId, long ownerId, int ownerType, long plid,
096 String portletId, String xml)
097 throws SystemException {
098
099 try {
100 Map<String, Preference> preferencesMap =
101 new HashMap<String, Preference>();
102
103 populateMap(xml, preferencesMap);
104
105 return new PortletPreferencesImpl(
106 companyId, ownerId, ownerType, plid, portletId, xml,
107 preferencesMap);
108 }
109 catch (SystemException se) {
110 throw se;
111 }
112 }
113
114 @Override
115 public PortalPreferencesImpl fromXML(
116 long companyId, long ownerId, int ownerType, String xml)
117 throws SystemException {
118
119 try {
120 Map<String, Preference> preferencesMap =
121 new HashMap<String, Preference>();
122
123 populateMap(xml, preferencesMap);
124
125 return new PortalPreferencesImpl(
126 companyId, ownerId, ownerType, xml, preferencesMap, false);
127 }
128 catch (SystemException se) {
129 throw se;
130 }
131 }
132
133 @Override
134 public PortletPreferences getLayoutPortletSetup(
135 Layout layout, String portletId)
136 throws SystemException {
137
138 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
139 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
140
141 if (PortletConstants.hasUserId(portletId)) {
142 ownerId = PortletConstants.getUserId(portletId);
143 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
144 }
145
146 return PortletPreferencesLocalServiceUtil.getPreferences(
147 layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
148 portletId);
149 }
150
151 @Override
152 public PortalPreferences getPortalPreferences(HttpServletRequest request)
153 throws SystemException {
154
155 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
156 WebKeys.THEME_DISPLAY);
157
158 return getPortalPreferences(
159 request.getSession(), themeDisplay.getCompanyId(),
160 themeDisplay.getUserId(), themeDisplay.isSignedIn());
161 }
162
163 @Override
164 public PortalPreferences getPortalPreferences(
165 HttpSession session, long companyId, long userId, boolean signedIn)
166 throws SystemException {
167
168 long ownerId = userId;
169 int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
170
171 PortalPreferences portalPreferences = null;
172
173 if (signedIn) {
174 PortalPreferencesWrapper portalPreferencesWrapper =
175 (PortalPreferencesWrapper)
176 PortalPreferencesLocalServiceUtil.getPreferences(
177 companyId, ownerId, ownerType);
178
179 portalPreferences =
180 portalPreferencesWrapper.getPortalPreferencesImpl();
181 }
182 else {
183 if (session != null) {
184 portalPreferences = (PortalPreferences)session.getAttribute(
185 WebKeys.PORTAL_PREFERENCES);
186 }
187
188 if (portalPreferences == null) {
189 PortalPreferencesWrapper portalPreferencesWrapper =
190 (PortalPreferencesWrapper)
191 PortalPreferencesLocalServiceUtil.getPreferences(
192 companyId, ownerId, ownerType);
193
194 PortalPreferencesImpl portalPreferencesImpl =
195 portalPreferencesWrapper.getPortalPreferencesImpl();
196
197 portalPreferences =
198 (PortalPreferences)portalPreferencesImpl.clone();
199
200 if (session != null) {
201 session.setAttribute(
202 WebKeys.PORTAL_PREFERENCES, portalPreferences);
203 }
204 }
205 }
206
207 portalPreferences.setSignedIn(signedIn);
208 portalPreferences.setUserId(userId);
209
210 return portalPreferences;
211 }
212
213 @Override
214 public PortalPreferences getPortalPreferences(
215 long companyId, long userId, boolean signedIn)
216 throws SystemException {
217
218 return getPortalPreferences(null, companyId, userId, signedIn);
219 }
220
221 @Override
222 public PortalPreferences getPortalPreferences(PortletRequest portletRequest)
223 throws SystemException {
224
225 HttpServletRequest request = PortalUtil.getHttpServletRequest(
226 portletRequest);
227
228 return getPortalPreferences(request);
229 }
230
231 @Override
232 public PortletPreferences getPortletPreferences(
233 HttpServletRequest request, String portletId)
234 throws PortalException, SystemException {
235
236 PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
237 request, portletId);
238
239 return PortletPreferencesLocalServiceUtil.getPreferences(
240 portletPreferencesIds);
241 }
242
243 @Override
244 public PortletPreferencesIds getPortletPreferencesIds(
245 HttpServletRequest request, Layout layout, String portletId)
246 throws PortalException, SystemException {
247
248 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
249 WebKeys.THEME_DISPLAY);
250
251 long scopeGroupId = PortalUtil.getScopeGroupId(
252 request, portletId, true);
253 long userId = PortalUtil.getUserId(request);
254 LayoutTypePortlet layoutTypePortlet =
255 themeDisplay.getLayoutTypePortlet();
256
257 boolean modeEditGuest = false;
258
259 String portletMode = ParamUtil.getString(request, "p_p_mode");
260
261 if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString()) ||
262 ((layoutTypePortlet != null) &&
263 layoutTypePortlet.hasModeEditGuestPortletId(portletId))) {
264
265 modeEditGuest = true;
266 }
267
268 return getPortletPreferencesIds(
269 scopeGroupId, userId, layout, portletId, modeEditGuest);
270 }
271
272 @Override
273 public PortletPreferencesIds getPortletPreferencesIds(
274 HttpServletRequest request, String portletId)
275 throws PortalException, SystemException {
276
277 Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
278
279 return getPortletPreferencesIds(request, layout, portletId);
280 }
281
282 @Override
283 public PortletPreferencesIds getPortletPreferencesIds(
284 long scopeGroupId, long userId, Layout layout, String portletId,
285 boolean modeEditGuest)
286 throws PortalException, SystemException {
287
288 PermissionChecker permissionChecker =
289 PermissionThreadLocal.getPermissionChecker();
290
291 String originalPortletId = portletId;
292
293 Portlet portlet = PortletLocalServiceUtil.getPortletById(
294 layout.getCompanyId(), portletId);
295
296 long ownerId = 0;
297 int ownerType = 0;
298 long plid = 0;
299
300 if (modeEditGuest) {
301 boolean hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
302 permissionChecker, layout, ActionKeys.UPDATE);
303
304 if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
305 }
306 else {
307
308
309
310
311 throw new PrincipalException();
312 }
313 }
314
315 if (PortletConstants.hasUserId(originalPortletId) &&
316 (PortletConstants.getUserId(originalPortletId) == userId)) {
317
318 ownerId = userId;
319 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
320 plid = layout.getPlid();
321 }
322 else if (portlet.isPreferencesCompanyWide()) {
323 ownerId = layout.getCompanyId();
324 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
325 plid = PortletKeys.PREFS_PLID_SHARED;
326 portletId = PortletConstants.getRootPortletId(portletId);
327 }
328 else {
329 if (portlet.isPreferencesUniquePerLayout()) {
330 ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
331 ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
332 plid = layout.getPlid();
333
334 if (portlet.isPreferencesOwnedByGroup()) {
335 }
336 else {
337 if ((userId <= 0) || modeEditGuest) {
338 userId = UserLocalServiceUtil.getDefaultUserId(
339 layout.getCompanyId());
340 }
341
342 ownerId = userId;
343 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
344 }
345 }
346 else {
347 plid = PortletKeys.PREFS_PLID_SHARED;
348
349 if (portlet.isPreferencesOwnedByGroup()) {
350 ownerId = scopeGroupId;
351 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
352 portletId = PortletConstants.getRootPortletId(portletId);
353 }
354 else {
355 if ((userId <= 0) || modeEditGuest) {
356 userId = UserLocalServiceUtil.getDefaultUserId(
357 layout.getCompanyId());
358 }
359
360 ownerId = userId;
361 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
362 }
363 }
364 }
365
366 return new PortletPreferencesIds(
367 layout.getCompanyId(), ownerId, ownerType, plid, portletId);
368 }
369
370 @Override
371 public PortletPreferences getPortletSetup(
372 HttpServletRequest request, String portletId)
373 throws PortalException, SystemException {
374
375 return getPortletSetup(request, portletId, null);
376 }
377
378 @Override
379 public PortletPreferences getPortletSetup(
380 HttpServletRequest request, String portletId,
381 String defaultPreferences)
382 throws PortalException, SystemException {
383
384 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
385 JavaConstants.JAVAX_PORTLET_REQUEST);
386
387 if (portletRequest instanceof ConfigurationPortletRequest) {
388 PortletRequestWrapper portletRequestWrapper =
389 (PortletRequestWrapper)portletRequest;
390
391 return portletRequestWrapper.getPreferences();
392 }
393
394 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
395 WebKeys.THEME_DISPLAY);
396
397 long scopeGroupId = PortalUtil.getScopeGroupId(
398 request, portletId, true);
399
400 return getPortletSetup(
401 scopeGroupId, themeDisplay.getLayout(), portletId,
402 defaultPreferences);
403 }
404
405 @Override
406 public PortletPreferences getPortletSetup(
407 Layout layout, String portletId, String defaultPreferences)
408 throws SystemException {
409
410 return getPortletSetup(
411 LayoutConstants.DEFAULT_PLID, layout, portletId,
412 defaultPreferences);
413 }
414
415 @Override
416 public PortletPreferences getPortletSetup(
417 long scopeGroupId, Layout layout, String portletId,
418 String defaultPreferences)
419 throws SystemException {
420
421 return getPortletSetup(
422 scopeGroupId, layout, portletId, defaultPreferences, false);
423 }
424
425 @Override
426 public PortletPreferences getPortletSetup(PortletRequest portletRequest)
427 throws PortalException, SystemException {
428
429 String portletId = PortalUtil.getPortletId(portletRequest);
430
431 return getPortletSetup(portletRequest, portletId);
432 }
433
434 @Override
435 public PortletPreferences getPortletSetup(
436 PortletRequest portletRequest, String portletId)
437 throws PortalException, SystemException {
438
439 if (portletRequest instanceof ConfigurationPortletRequest) {
440 PortletRequestWrapper portletRequestWrapper =
441 (PortletRequestWrapper)portletRequest;
442
443 return portletRequestWrapper.getPreferences();
444 }
445
446 HttpServletRequest request = PortalUtil.getHttpServletRequest(
447 portletRequest);
448
449 return getPortletSetup(request, portletId);
450 }
451
452 @Override
453 public Map<Long, PortletPreferences> getPortletSetupMap(
454 long companyId, long groupId, long ownerId, int ownerType,
455 String portletId, boolean privateLayout)
456 throws SystemException {
457
458 Map<Long, PortletPreferences> portletSetupMap =
459 new HashMap<Long, PortletPreferences>();
460
461 List<com.liferay.portal.model.PortletPreferences>
462 portletPreferencesList =
463 PortletPreferencesLocalServiceUtil.getPortletPreferences(
464 companyId, groupId, ownerId, ownerType, portletId,
465 privateLayout);
466
467 for (com.liferay.portal.model.PortletPreferences portletPreferences :
468 portletPreferencesList) {
469
470 PortletPreferences portletSetup =
471 PortletPreferencesLocalServiceUtil.getPreferences(
472 companyId, ownerId, ownerType, portletPreferences.getPlid(),
473 portletId);
474
475 portletSetupMap.put(portletPreferences.getPlid(), portletSetup);
476 }
477
478 return portletSetupMap;
479 }
480
481 @Override
482 public PortletPreferences getPreferences(HttpServletRequest request) {
483 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
484 JavaConstants.JAVAX_PORTLET_REQUEST);
485
486 PortletPreferences portletPreferences = null;
487
488 if (portletRequest != null) {
489 PortletPreferencesWrapper portletPreferencesWrapper =
490 (PortletPreferencesWrapper)portletRequest.getPreferences();
491
492 portletPreferences =
493 portletPreferencesWrapper.getPortletPreferencesImpl();
494 }
495
496 return portletPreferences;
497 }
498
499 @Override
500 public PreferencesValidator getPreferencesValidator(Portlet portlet) {
501 return PortalUtil.getPreferencesValidator(portlet);
502 }
503
504 @Override
505 public PortletPreferences getStrictLayoutPortletSetup(
506 Layout layout, String portletId)
507 throws SystemException {
508
509 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
510 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
511
512 if (PortletConstants.hasUserId(portletId)) {
513 ownerId = PortletConstants.getUserId(portletId);
514 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
515 }
516
517 return PortletPreferencesLocalServiceUtil.getStrictPreferences(
518 layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
519 portletId);
520 }
521
522 @Override
523 public PortletPreferences getStrictPortletSetup(
524 Layout layout, String portletId)
525 throws SystemException {
526
527 return getPortletSetup(
528 LayoutConstants.DEFAULT_PLID, layout, portletId, StringPool.BLANK,
529 true);
530 }
531
532 @Override
533 public String toXML(PortalPreferences portalPreferences) {
534 PortalPreferencesImpl portalPreferencesImpl =
535 (PortalPreferencesImpl)portalPreferences;
536
537 return portalPreferencesImpl.toXML();
538 }
539
540 @Override
541 public String toXML(PortletPreferences portletPreferences) {
542 PortletPreferencesImpl portletPreferencesImpl =
543 (PortletPreferencesImpl)portletPreferences;
544
545 return portletPreferencesImpl.toXML();
546 }
547
548 protected PortletPreferences getPortletSetup(
549 long scopeGroupId, Layout layout, String portletId,
550 String defaultPreferences, boolean strictMode)
551 throws SystemException {
552
553 String originalPortletId = portletId;
554
555 Portlet portlet = PortletLocalServiceUtil.getPortletById(
556 layout.getCompanyId(), portletId);
557
558 boolean uniquePerLayout = false;
559 boolean uniquePerGroup = false;
560
561 if (portlet.isPreferencesCompanyWide()) {
562 portletId = PortletConstants.getRootPortletId(portletId);
563 }
564 else {
565 if (portlet.isPreferencesUniquePerLayout()) {
566 uniquePerLayout = true;
567
568 if (portlet.isPreferencesOwnedByGroup()) {
569 uniquePerGroup = true;
570 }
571 }
572 else {
573 if (portlet.isPreferencesOwnedByGroup()) {
574 uniquePerGroup = true;
575 portletId = PortletConstants.getRootPortletId(portletId);
576 }
577 }
578 }
579
580 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
581 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
582 long plid = layout.getPlid();
583
584 Group group = GroupLocalServiceUtil.fetchGroup(scopeGroupId);
585
586 if ((group != null) && group.isLayout()) {
587 plid = group.getClassPK();
588 }
589
590 if (PortletConstants.hasUserId(originalPortletId)) {
591 ownerId = PortletConstants.getUserId(originalPortletId);
592 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
593 }
594 else if (!uniquePerLayout) {
595 plid = PortletKeys.PREFS_PLID_SHARED;
596
597 if (uniquePerGroup) {
598 if (scopeGroupId > LayoutConstants.DEFAULT_PLID) {
599 ownerId = scopeGroupId;
600 }
601 else {
602 ownerId = layout.getGroupId();
603 }
604
605 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
606 }
607 else {
608 ownerId = layout.getCompanyId();
609 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
610 }
611 }
612
613 if (strictMode) {
614 return PortletPreferencesLocalServiceUtil.getStrictPreferences(
615 layout.getCompanyId(), ownerId, ownerType, plid, portletId);
616 }
617
618 return PortletPreferencesLocalServiceUtil.getPreferences(
619 layout.getCompanyId(), ownerId, ownerType, plid, portletId,
620 defaultPreferences);
621 }
622
623 protected void populateMap(
624 String xml, Map<String, Preference> preferencesMap)
625 throws SystemException {
626
627 if (Validator.isNull(xml)) {
628 return;
629 }
630
631 XMLEventReader xmlEventReader = null;
632
633 try {
634 XMLInputFactory xmlInputFactory =
635 StAXReaderUtil.getXMLInputFactory();
636
637 xmlEventReader = xmlInputFactory.createXMLEventReader(
638 new UnsyncStringReader(xml));
639
640 while (xmlEventReader.hasNext()) {
641 XMLEvent xmlEvent = xmlEventReader.nextEvent();
642
643 if (xmlEvent.isStartElement()) {
644 StartElement startElement = xmlEvent.asStartElement();
645
646 String elementName = startElement.getName().getLocalPart();
647
648 if (elementName.equals("preference")) {
649 Preference preference = readPreference(xmlEventReader);
650
651 preferencesMap.put(preference.getName(), preference);
652 }
653 }
654 }
655 }
656 catch (XMLStreamException xse) {
657 throw new SystemException(xse);
658 }
659 finally {
660 if (xmlEventReader != null) {
661 try {
662 xmlEventReader.close();
663 }
664 catch (XMLStreamException xse) {
665 }
666 }
667 }
668 }
669
670 protected Preference readPreference(XMLEventReader xmlEventReader)
671 throws XMLStreamException {
672
673 String name = null;
674 List<String> values = new ArrayList<String>();
675 boolean readOnly = false;
676
677 while (xmlEventReader.hasNext()) {
678 XMLEvent xmlEvent = xmlEventReader.nextEvent();
679
680 if (xmlEvent.isStartElement()) {
681 StartElement startElement = xmlEvent.asStartElement();
682
683 String elementName = startElement.getName().getLocalPart();
684
685 if (elementName.equals("name")) {
686 name = StAXReaderUtil.read(xmlEventReader);
687 }
688 else if (elementName.equals("value")) {
689 String value = StAXReaderUtil.read(xmlEventReader);
690
691 values.add(value);
692 }
693 else if (elementName.equals("read-only")) {
694 String value = StAXReaderUtil.read(xmlEventReader);
695
696 readOnly = GetterUtil.getBoolean(value);
697 }
698 }
699 else if (xmlEvent.isEndElement()) {
700 EndElement endElement = xmlEvent.asEndElement();
701
702 String elementName = endElement.getName().getLocalPart();
703
704 if (elementName.equals("preference")) {
705 break;
706 }
707 }
708 }
709
710 return new Preference(
711 name, values.toArray(new String[values.size()]), readOnly);
712 }
713
714 }