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