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.portal.service;
016    
017    import com.liferay.portal.NoSuchUserException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.upload.UploadPortletRequest;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.GroupConstants;
029    import com.liferay.portal.model.PortletPreferencesIds;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portlet.PortletPreferencesFactoryUtil;
034    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
035    
036    import java.io.Serializable;
037    
038    import java.util.ArrayList;
039    import java.util.Date;
040    import java.util.Enumeration;
041    import java.util.HashMap;
042    import java.util.List;
043    import java.util.Map;
044    
045    import javax.portlet.PortletRequest;
046    
047    import javax.servlet.http.HttpServletRequest;
048    
049    /**
050     * @author Brian Wing Shun Chan
051     * @author Raymond Aug??
052     */
053    public class ServiceContextFactory {
054    
055            public static ServiceContext getInstance(HttpServletRequest request)
056                    throws PortalException {
057    
058                    ServiceContext serviceContext = new ServiceContext();
059    
060                    // Theme display
061    
062                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
063                            WebKeys.THEME_DISPLAY);
064    
065                    if (themeDisplay != null) {
066                            serviceContext.setCompanyId(themeDisplay.getCompanyId());
067                            serviceContext.setLanguageId(themeDisplay.getLanguageId());
068                            serviceContext.setLayoutFullURL(
069                                    PortalUtil.getCanonicalURL(
070                                            PortalUtil.getLayoutFullURL(themeDisplay), themeDisplay,
071                                            themeDisplay.getLayout(), true));
072                            serviceContext.setLayoutURL(
073                                    PortalUtil.getCanonicalURL(
074                                            PortalUtil.getLayoutURL(themeDisplay), themeDisplay,
075                                            themeDisplay.getLayout(), true));
076                            serviceContext.setPlid(themeDisplay.getPlid());
077                            serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
078                            serviceContext.setSignedIn(themeDisplay.isSignedIn());
079                            serviceContext.setTimeZone(themeDisplay.getTimeZone());
080    
081                            User user = themeDisplay.getUser();
082    
083                            serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
084                            serviceContext.setUserId(user.getUserId());
085                    }
086                    else {
087                            long companyId = PortalUtil.getCompanyId(request);
088    
089                            serviceContext.setCompanyId(companyId);
090    
091                            Group guestGroup = GroupLocalServiceUtil.getGroup(
092                                    serviceContext.getCompanyId(), GroupConstants.GUEST);
093    
094                            serviceContext.setScopeGroupId(guestGroup.getGroupId());
095    
096                            long plid = LayoutLocalServiceUtil.getDefaultPlid(
097                                    serviceContext.getScopeGroupId(), false);
098    
099                            serviceContext.setPlid(plid);
100    
101                            User user = null;
102    
103                            try {
104                                    user = PortalUtil.getUser(request);
105                            }
106                            catch (NoSuchUserException nsue) {
107    
108                                    // LPS-24160
109    
110                            }
111    
112                            if (user != null) {
113                                    serviceContext.setSignedIn(!user.isDefaultUser());
114                                    serviceContext.setUserId(user.getUserId());
115                            }
116                            else {
117                                    serviceContext.setSignedIn(false);
118                            }
119                    }
120    
121                    serviceContext.setPortalURL(PortalUtil.getPortalURL(request));
122                    serviceContext.setPathMain(PortalUtil.getPathMain());
123                    serviceContext.setPathFriendlyURLPrivateGroup(
124                            PortalUtil.getPathFriendlyURLPrivateGroup());
125                    serviceContext.setPathFriendlyURLPrivateUser(
126                            PortalUtil.getPathFriendlyURLPrivateUser());
127                    serviceContext.setPathFriendlyURLPublic(
128                            PortalUtil.getPathFriendlyURLPublic());
129    
130                    // Attributes
131    
132                    Map<String, Serializable> attributes = new HashMap<>();
133    
134                    Map<String, String[]> parameters = request.getParameterMap();
135    
136                    for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
137                            String name = entry.getKey();
138                            String[] values = entry.getValue();
139    
140                            if (ArrayUtil.isNotEmpty(values)) {
141                                    if (values.length == 1) {
142                                            attributes.put(name, values[0]);
143                                    }
144                                    else {
145                                            attributes.put(name, values);
146                                    }
147                            }
148                    }
149    
150                    serviceContext.setAttributes(attributes);
151    
152                    // Command
153    
154                    String cmd = ParamUtil.getString(request, Constants.CMD);
155    
156                    serviceContext.setCommand(cmd);
157    
158                    // Current URL
159    
160                    String currentURL = PortalUtil.getCurrentURL(request);
161    
162                    serviceContext.setCurrentURL(currentURL);
163    
164                    // Form date
165    
166                    long formDateLong = ParamUtil.getLong(request, "formDate");
167    
168                    if (formDateLong > 0) {
169                            Date formDate = new Date(formDateLong);
170    
171                            serviceContext.setFormDate(formDate);
172                    }
173    
174                    // Permissions
175    
176                    boolean addGroupPermissions = ParamUtil.getBoolean(
177                            request, "addGroupPermissions");
178                    boolean addGuestPermissions = ParamUtil.getBoolean(
179                            request, "addGuestPermissions");
180                    String[] groupPermissions = PortalUtil.getGroupPermissions(request);
181                    String[] guestPermissions = PortalUtil.getGuestPermissions(request);
182    
183                    serviceContext.setAddGroupPermissions(addGroupPermissions);
184                    serviceContext.setAddGuestPermissions(addGuestPermissions);
185                    serviceContext.setGroupPermissions(groupPermissions);
186                    serviceContext.setGuestPermissions(guestPermissions);
187    
188                    // Portlet preferences ids
189    
190                    String portletId = PortalUtil.getPortletId(request);
191    
192                    if (Validator.isNotNull(portletId)) {
193                            PortletPreferencesIds portletPreferencesIds =
194                                    PortletPreferencesFactoryUtil.getPortletPreferencesIds(
195                                            request, portletId);
196    
197                            serviceContext.setPortletPreferencesIds(portletPreferencesIds);
198                    }
199    
200                    // Request
201    
202                    Map<String, String> headerMap = new HashMap<>();
203    
204                    Enumeration<String> enu = request.getHeaderNames();
205    
206                    while (enu.hasMoreElements()) {
207                            String header = enu.nextElement();
208    
209                            String value = request.getHeader(header);
210    
211                            headerMap.put(header, value);
212                    }
213    
214                    serviceContext.setHeaders(headerMap);
215    
216                    serviceContext.setRemoteAddr(request.getRemoteAddr());
217                    serviceContext.setRemoteHost(request.getRemoteHost());
218                    serviceContext.setRequest(request);
219    
220                    // Asset
221    
222                    Map<String, String[]> parameterMap = request.getParameterMap();
223    
224                    List<Long> assetCategoryIdsList = new ArrayList<>();
225    
226                    boolean updateAssetCategoryIds = false;
227    
228                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
229                            String name = entry.getKey();
230    
231                            if (name.startsWith("assetCategoryIds")) {
232                                    updateAssetCategoryIds = true;
233    
234                                    long[] assetVocabularyAssetCategoryIds = StringUtil.split(
235                                            ParamUtil.getString(request, name), 0L);
236    
237                                    for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
238                                            assetCategoryIdsList.add(assetCategoryId);
239                                    }
240                            }
241                    }
242    
243                    if (updateAssetCategoryIds) {
244                            long[] assetCategoryIds = ArrayUtil.toArray(
245                                    assetCategoryIdsList.toArray(
246                                            new Long[assetCategoryIdsList.size()]));
247    
248                            serviceContext.setAssetCategoryIds(assetCategoryIds);
249                    }
250    
251                    boolean assetEntryVisible = ParamUtil.getBoolean(
252                            request, "assetEntryVisible", true);
253    
254                    serviceContext.setAssetEntryVisible(assetEntryVisible);
255    
256                    String assetLinkEntryIdsString = request.getParameter(
257                            "assetLinksSearchContainerPrimaryKeys");
258    
259                    if (assetLinkEntryIdsString != null) {
260                            long[] assetLinkEntryIds = StringUtil.split(
261                                    assetLinkEntryIdsString, 0L);
262    
263                            serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
264                    }
265    
266                    String assetTagNamesString = request.getParameter("assetTagNames");
267    
268                    if (assetTagNamesString != null) {
269                            String[] assetTagNames = StringUtil.split(assetTagNamesString);
270    
271                            serviceContext.setAssetTagNames(assetTagNames);
272                    }
273    
274                    // Workflow
275    
276                    int workflowAction = ParamUtil.getInteger(
277                            request, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
278    
279                    serviceContext.setWorkflowAction(workflowAction);
280    
281                    return serviceContext;
282            }
283    
284            public static ServiceContext getInstance(PortletRequest portletRequest)
285                    throws PortalException {
286    
287                    // Theme display
288    
289                    ServiceContext serviceContext =
290                            ServiceContextThreadLocal.getServiceContext();
291    
292                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
293                            WebKeys.THEME_DISPLAY);
294    
295                    if (serviceContext != null) {
296                            serviceContext = (ServiceContext)serviceContext.clone();
297                    }
298                    else {
299                            serviceContext = new ServiceContext();
300    
301                            serviceContext.setCompanyId(themeDisplay.getCompanyId());
302                            serviceContext.setLanguageId(themeDisplay.getLanguageId());
303                            serviceContext.setLayoutFullURL(
304                                    PortalUtil.getLayoutFullURL(themeDisplay));
305                            serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
306                            serviceContext.setPathFriendlyURLPrivateGroup(
307                                    PortalUtil.getPathFriendlyURLPrivateGroup());
308                            serviceContext.setPathFriendlyURLPrivateUser(
309                                    PortalUtil.getPathFriendlyURLPrivateUser());
310                            serviceContext.setPathFriendlyURLPublic(
311                                    PortalUtil.getPathFriendlyURLPublic());
312                            serviceContext.setPathMain(PortalUtil.getPathMain());
313                            serviceContext.setPlid(themeDisplay.getPlid());
314                            serviceContext.setPortalURL(
315                                    PortalUtil.getPortalURL(portletRequest));
316                            serviceContext.setSignedIn(themeDisplay.isSignedIn());
317                            serviceContext.setTimeZone(themeDisplay.getTimeZone());
318    
319                            User user = themeDisplay.getUser();
320    
321                            serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
322                            serviceContext.setUserId(user.getUserId());
323                    }
324    
325                    serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
326    
327                    // Attributes
328    
329                    Map<String, Serializable> attributes = new HashMap<>();
330    
331                    Enumeration<String> enu = portletRequest.getParameterNames();
332    
333                    while (enu.hasMoreElements()) {
334                            String param = enu.nextElement();
335    
336                            String[] values = portletRequest.getParameterValues(param);
337    
338                            if (ArrayUtil.isNotEmpty(values)) {
339                                    if (values.length == 1) {
340                                            attributes.put(param, values[0]);
341                                    }
342                                    else {
343                                            attributes.put(param, values);
344                                    }
345                            }
346                    }
347    
348                    serviceContext.setAttributes(attributes);
349    
350                    // Command
351    
352                    String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
353    
354                    serviceContext.setCommand(cmd);
355    
356                    // Current URL
357    
358                    String currentURL = PortalUtil.getCurrentURL(portletRequest);
359    
360                    serviceContext.setCurrentURL(currentURL);
361    
362                    // Form date
363    
364                    long formDateLong = ParamUtil.getLong(portletRequest, "formDate");
365    
366                    if (formDateLong > 0) {
367                            Date formDate = new Date(formDateLong);
368    
369                            serviceContext.setFormDate(formDate);
370                    }
371    
372                    // Permissions
373    
374                    boolean addGroupPermissions = ParamUtil.getBoolean(
375                            portletRequest, "addGroupPermissions");
376                    boolean addGuestPermissions = ParamUtil.getBoolean(
377                            portletRequest, "addGuestPermissions");
378                    String[] groupPermissions = PortalUtil.getGroupPermissions(
379                            portletRequest);
380                    String[] guestPermissions = PortalUtil.getGuestPermissions(
381                            portletRequest);
382    
383                    serviceContext.setAddGroupPermissions(addGroupPermissions);
384                    serviceContext.setAddGuestPermissions(addGuestPermissions);
385                    serviceContext.setGroupPermissions(groupPermissions);
386                    serviceContext.setGuestPermissions(guestPermissions);
387    
388                    // Portlet preferences ids
389    
390                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
391                            portletRequest);
392    
393                    String portletId = PortalUtil.getPortletId(portletRequest);
394    
395                    PortletPreferencesIds portletPreferencesIds =
396                            PortletPreferencesFactoryUtil.getPortletPreferencesIds(
397                                    request, portletId);
398    
399                    serviceContext.setPortletPreferencesIds(portletPreferencesIds);
400    
401                    // Request
402    
403                    Map<String, String> headerMap = new HashMap<>();
404    
405                    enu = request.getHeaderNames();
406    
407                    while (enu.hasMoreElements()) {
408                            String header = enu.nextElement();
409    
410                            String value = request.getHeader(header);
411    
412                            headerMap.put(header, value);
413                    }
414    
415                    serviceContext.setHeaders(headerMap);
416    
417                    serviceContext.setRemoteAddr(request.getRemoteAddr());
418                    serviceContext.setRemoteHost(request.getRemoteHost());
419                    serviceContext.setRequest(request);
420    
421                    // Asset
422    
423                    Map<String, String[]> parameterMap = portletRequest.getParameterMap();
424    
425                    List<Long> assetCategoryIdsList = new ArrayList<>();
426    
427                    boolean updateAssetCategoryIds = false;
428    
429                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
430                            String name = entry.getKey();
431    
432                            if (name.startsWith("assetCategoryIds")) {
433                                    updateAssetCategoryIds = true;
434    
435                                    long[] assetVocabularyAssetCategoryIds = StringUtil.split(
436                                            ParamUtil.getString(portletRequest, name), 0L);
437    
438                                    for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
439                                            assetCategoryIdsList.add(assetCategoryId);
440                                    }
441                            }
442                    }
443    
444                    if (updateAssetCategoryIds) {
445                            long[] assetCategoryIds = ArrayUtil.toArray(
446                                    assetCategoryIdsList.toArray(
447                                            new Long[assetCategoryIdsList.size()]));
448    
449                            serviceContext.setAssetCategoryIds(assetCategoryIds);
450                    }
451    
452                    boolean assetEntryVisible = ParamUtil.getBoolean(
453                            portletRequest, "assetEntryVisible", true);
454    
455                    serviceContext.setAssetEntryVisible(assetEntryVisible);
456    
457                    String assetLinkEntryIdsString = request.getParameter(
458                            "assetLinksSearchContainerPrimaryKeys");
459    
460                    if (assetLinkEntryIdsString != null) {
461                            long[] assetLinkEntryIds = StringUtil.split(
462                                    assetLinkEntryIdsString, 0L);
463    
464                            serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
465                    }
466    
467                    String assetTagNamesString = request.getParameter("assetTagNames");
468    
469                    if (assetTagNamesString != null) {
470                            String[] assetTagNames = StringUtil.split(assetTagNamesString);
471    
472                            serviceContext.setAssetTagNames(assetTagNames);
473                    }
474    
475                    // Workflow
476    
477                    int workflowAction = ParamUtil.getInteger(
478                            portletRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
479    
480                    serviceContext.setWorkflowAction(workflowAction);
481    
482                    return serviceContext;
483            }
484    
485            public static ServiceContext getInstance(
486                            String className, PortletRequest portletRequest)
487                    throws PortalException {
488    
489                    ServiceContext serviceContext = getInstance(portletRequest);
490    
491                    // Permissions
492    
493                    String[] groupPermissions = PortalUtil.getGroupPermissions(
494                            portletRequest, className);
495                    String[] guestPermissions = PortalUtil.getGuestPermissions(
496                            portletRequest, className);
497    
498                    if (groupPermissions != null) {
499                            serviceContext.setGroupPermissions(groupPermissions);
500                    }
501    
502                    if (guestPermissions != null) {
503                            serviceContext.setGuestPermissions(guestPermissions);
504                    }
505    
506                    // Expando
507    
508                    Map<String, Serializable> expandoBridgeAttributes =
509                            PortalUtil.getExpandoBridgeAttributes(
510                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
511                                            serviceContext.getCompanyId(), className),
512                                    portletRequest);
513    
514                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
515    
516                    return serviceContext;
517            }
518    
519            public static ServiceContext getInstance(
520                            String className, UploadPortletRequest uploadPortletRequest)
521                    throws PortalException {
522    
523                    ServiceContext serviceContext = getInstance(uploadPortletRequest);
524    
525                    // Permissions
526    
527                    String[] groupPermissions = PortalUtil.getGroupPermissions(
528                            uploadPortletRequest, className);
529                    String[] guestPermissions = PortalUtil.getGuestPermissions(
530                            uploadPortletRequest, className);
531    
532                    if (groupPermissions != null) {
533                            serviceContext.setGroupPermissions(groupPermissions);
534                    }
535    
536                    if (guestPermissions != null) {
537                            serviceContext.setGuestPermissions(guestPermissions);
538                    }
539    
540                    // Expando
541    
542                    Map<String, Serializable> expandoBridgeAttributes =
543                            PortalUtil.getExpandoBridgeAttributes(
544                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
545                                            serviceContext.getCompanyId(), className),
546                                    uploadPortletRequest);
547    
548                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
549    
550                    return serviceContext;
551            }
552    
553    }