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 =
133                            new HashMap<String, Serializable>();
134    
135                    Map<String, String[]> parameters = request.getParameterMap();
136    
137                    for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
138                            String name = entry.getKey();
139                            String[] values = entry.getValue();
140    
141                            if (ArrayUtil.isNotEmpty(values)) {
142                                    if (values.length == 1) {
143                                            attributes.put(name, values[0]);
144                                    }
145                                    else {
146                                            attributes.put(name, values);
147                                    }
148                            }
149                    }
150    
151                    serviceContext.setAttributes(attributes);
152    
153                    // Command
154    
155                    String cmd = ParamUtil.getString(request, Constants.CMD);
156    
157                    serviceContext.setCommand(cmd);
158    
159                    // Current URL
160    
161                    String currentURL = PortalUtil.getCurrentURL(request);
162    
163                    serviceContext.setCurrentURL(currentURL);
164    
165                    // Form date
166    
167                    long formDateLong = ParamUtil.getLong(request, "formDate");
168    
169                    if (formDateLong > 0) {
170                            Date formDate = new Date(formDateLong);
171    
172                            serviceContext.setFormDate(formDate);
173                    }
174    
175                    // Permissions
176    
177                    boolean addGroupPermissions = ParamUtil.getBoolean(
178                            request, "addGroupPermissions");
179                    boolean addGuestPermissions = ParamUtil.getBoolean(
180                            request, "addGuestPermissions");
181                    String[] groupPermissions = PortalUtil.getGroupPermissions(request);
182                    String[] guestPermissions = PortalUtil.getGuestPermissions(request);
183    
184                    serviceContext.setAddGroupPermissions(addGroupPermissions);
185                    serviceContext.setAddGuestPermissions(addGuestPermissions);
186                    serviceContext.setGroupPermissions(groupPermissions);
187                    serviceContext.setGuestPermissions(guestPermissions);
188    
189                    // Portlet preferences ids
190    
191                    String portletId = PortalUtil.getPortletId(request);
192    
193                    if (Validator.isNotNull(portletId)) {
194                            PortletPreferencesIds portletPreferencesIds =
195                                    PortletPreferencesFactoryUtil.getPortletPreferencesIds(
196                                            request, portletId);
197    
198                            serviceContext.setPortletPreferencesIds(portletPreferencesIds);
199                    }
200    
201                    // Request
202    
203                    Map<String, String> headerMap = new HashMap<String, String>();
204    
205                    Enumeration<String> enu = request.getHeaderNames();
206    
207                    while (enu.hasMoreElements()) {
208                            String header = enu.nextElement();
209    
210                            String value = request.getHeader(header);
211    
212                            headerMap.put(header, value);
213                    }
214    
215                    serviceContext.setHeaders(headerMap);
216    
217                    serviceContext.setRemoteAddr(request.getRemoteAddr());
218                    serviceContext.setRemoteHost(request.getRemoteHost());
219                    serviceContext.setRequest(request);
220    
221                    // Asset
222    
223                    Map<String, String[]> parameterMap = request.getParameterMap();
224    
225                    List<Long> assetCategoryIdsList = new ArrayList<Long>();
226    
227                    boolean updateAssetCategoryIds = false;
228    
229                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
230                            String name = entry.getKey();
231    
232                            if (name.startsWith("assetCategoryIds")) {
233                                    updateAssetCategoryIds = true;
234    
235                                    long[] assetVocabularyAssetCategoryIds = StringUtil.split(
236                                            ParamUtil.getString(request, name), 0L);
237    
238                                    for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
239                                            assetCategoryIdsList.add(assetCategoryId);
240                                    }
241                            }
242                    }
243    
244                    if (updateAssetCategoryIds) {
245                            long[] assetCategoryIds = ArrayUtil.toArray(
246                                    assetCategoryIdsList.toArray(
247                                            new Long[assetCategoryIdsList.size()]));
248    
249                            serviceContext.setAssetCategoryIds(assetCategoryIds);
250                    }
251    
252                    boolean assetEntryVisible = ParamUtil.getBoolean(
253                            request, "assetEntryVisible", true);
254    
255                    serviceContext.setAssetEntryVisible(assetEntryVisible);
256    
257                    String assetLinkEntryIdsString = request.getParameter(
258                            "assetLinksSearchContainerPrimaryKeys");
259    
260                    if (assetLinkEntryIdsString != null) {
261                            long[] assetLinkEntryIds = StringUtil.split(
262                                    assetLinkEntryIdsString, 0L);
263    
264                            serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
265                    }
266    
267                    String assetTagNamesString = request.getParameter("assetTagNames");
268    
269                    if (assetTagNamesString != null) {
270                            String[] assetTagNames = StringUtil.split(assetTagNamesString);
271    
272                            serviceContext.setAssetTagNames(assetTagNames);
273                    }
274    
275                    // Workflow
276    
277                    int workflowAction = ParamUtil.getInteger(
278                            request, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
279    
280                    serviceContext.setWorkflowAction(workflowAction);
281    
282                    return serviceContext;
283            }
284    
285            public static ServiceContext getInstance(PortletRequest portletRequest)
286                    throws PortalException {
287    
288                    // Theme display
289    
290                    ServiceContext serviceContext =
291                            ServiceContextThreadLocal.getServiceContext();
292    
293                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
294                            WebKeys.THEME_DISPLAY);
295    
296                    if (serviceContext != null) {
297                            serviceContext = (ServiceContext)serviceContext.clone();
298                    }
299                    else {
300                            serviceContext = new ServiceContext();
301    
302                            serviceContext.setCompanyId(themeDisplay.getCompanyId());
303                            serviceContext.setLanguageId(themeDisplay.getLanguageId());
304                            serviceContext.setLayoutFullURL(
305                                    PortalUtil.getLayoutFullURL(themeDisplay));
306                            serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
307                            serviceContext.setPathFriendlyURLPrivateGroup(
308                                    PortalUtil.getPathFriendlyURLPrivateGroup());
309                            serviceContext.setPathFriendlyURLPrivateUser(
310                                    PortalUtil.getPathFriendlyURLPrivateUser());
311                            serviceContext.setPathFriendlyURLPublic(
312                                    PortalUtil.getPathFriendlyURLPublic());
313                            serviceContext.setPathMain(PortalUtil.getPathMain());
314                            serviceContext.setPlid(themeDisplay.getPlid());
315                            serviceContext.setPortalURL(
316                                    PortalUtil.getPortalURL(portletRequest));
317                            serviceContext.setSignedIn(themeDisplay.isSignedIn());
318                            serviceContext.setTimeZone(themeDisplay.getTimeZone());
319    
320                            User user = themeDisplay.getUser();
321    
322                            serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
323                            serviceContext.setUserId(user.getUserId());
324                    }
325    
326                    serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
327    
328                    // Attributes
329    
330                    Map<String, Serializable> attributes =
331                            new HashMap<String, Serializable>();
332    
333                    Enumeration<String> enu = portletRequest.getParameterNames();
334    
335                    while (enu.hasMoreElements()) {
336                            String param = enu.nextElement();
337    
338                            String[] values = portletRequest.getParameterValues(param);
339    
340                            if (ArrayUtil.isNotEmpty(values)) {
341                                    if (values.length == 1) {
342                                            attributes.put(param, values[0]);
343                                    }
344                                    else {
345                                            attributes.put(param, values);
346                                    }
347                            }
348                    }
349    
350                    serviceContext.setAttributes(attributes);
351    
352                    // Command
353    
354                    String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
355    
356                    serviceContext.setCommand(cmd);
357    
358                    // Current URL
359    
360                    String currentURL = PortalUtil.getCurrentURL(portletRequest);
361    
362                    serviceContext.setCurrentURL(currentURL);
363    
364                    // Form date
365    
366                    long formDateLong = ParamUtil.getLong(portletRequest, "formDate");
367    
368                    if (formDateLong > 0) {
369                            Date formDate = new Date(formDateLong);
370    
371                            serviceContext.setFormDate(formDate);
372                    }
373    
374                    // Permissions
375    
376                    boolean addGroupPermissions = ParamUtil.getBoolean(
377                            portletRequest, "addGroupPermissions");
378                    boolean addGuestPermissions = ParamUtil.getBoolean(
379                            portletRequest, "addGuestPermissions");
380                    String[] groupPermissions = PortalUtil.getGroupPermissions(
381                            portletRequest);
382                    String[] guestPermissions = PortalUtil.getGuestPermissions(
383                            portletRequest);
384    
385                    serviceContext.setAddGroupPermissions(addGroupPermissions);
386                    serviceContext.setAddGuestPermissions(addGuestPermissions);
387                    serviceContext.setGroupPermissions(groupPermissions);
388                    serviceContext.setGuestPermissions(guestPermissions);
389    
390                    // Portlet preferences ids
391    
392                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
393                            portletRequest);
394    
395                    String portletId = PortalUtil.getPortletId(portletRequest);
396    
397                    PortletPreferencesIds portletPreferencesIds =
398                            PortletPreferencesFactoryUtil.getPortletPreferencesIds(
399                                    request, portletId);
400    
401                    serviceContext.setPortletPreferencesIds(portletPreferencesIds);
402    
403                    // Request
404    
405                    Map<String, String> headerMap = new HashMap<String, String>();
406    
407                    enu = request.getHeaderNames();
408    
409                    while (enu.hasMoreElements()) {
410                            String header = enu.nextElement();
411    
412                            String value = request.getHeader(header);
413    
414                            headerMap.put(header, value);
415                    }
416    
417                    serviceContext.setHeaders(headerMap);
418    
419                    serviceContext.setRemoteAddr(request.getRemoteAddr());
420                    serviceContext.setRemoteHost(request.getRemoteHost());
421                    serviceContext.setRequest(request);
422    
423                    // Asset
424    
425                    Map<String, String[]> parameterMap = portletRequest.getParameterMap();
426    
427                    List<Long> assetCategoryIdsList = new ArrayList<Long>();
428    
429                    boolean updateAssetCategoryIds = false;
430    
431                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
432                            String name = entry.getKey();
433    
434                            if (name.startsWith("assetCategoryIds")) {
435                                    updateAssetCategoryIds = true;
436    
437                                    long[] assetVocabularyAssetCategoryIds = StringUtil.split(
438                                            ParamUtil.getString(portletRequest, name), 0L);
439    
440                                    for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
441                                            assetCategoryIdsList.add(assetCategoryId);
442                                    }
443                            }
444                    }
445    
446                    if (updateAssetCategoryIds) {
447                            long[] assetCategoryIds = ArrayUtil.toArray(
448                                    assetCategoryIdsList.toArray(
449                                            new Long[assetCategoryIdsList.size()]));
450    
451                            serviceContext.setAssetCategoryIds(assetCategoryIds);
452                    }
453    
454                    boolean assetEntryVisible = ParamUtil.getBoolean(
455                            portletRequest, "assetEntryVisible", true);
456    
457                    serviceContext.setAssetEntryVisible(assetEntryVisible);
458    
459                    String assetLinkEntryIdsString = request.getParameter(
460                            "assetLinksSearchContainerPrimaryKeys");
461    
462                    if (assetLinkEntryIdsString != null) {
463                            long[] assetLinkEntryIds = StringUtil.split(
464                                    assetLinkEntryIdsString, 0L);
465    
466                            serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
467                    }
468    
469                    String assetTagNamesString = request.getParameter("assetTagNames");
470    
471                    if (assetTagNamesString != null) {
472                            String[] assetTagNames = StringUtil.split(assetTagNamesString);
473    
474                            serviceContext.setAssetTagNames(assetTagNames);
475                    }
476    
477                    // Workflow
478    
479                    int workflowAction = ParamUtil.getInteger(
480                            portletRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
481    
482                    serviceContext.setWorkflowAction(workflowAction);
483    
484                    return serviceContext;
485            }
486    
487            public static ServiceContext getInstance(
488                            String className, PortletRequest portletRequest)
489                    throws PortalException {
490    
491                    ServiceContext serviceContext = getInstance(portletRequest);
492    
493                    // Permissions
494    
495                    String[] groupPermissions = PortalUtil.getGroupPermissions(
496                            portletRequest, className);
497                    String[] guestPermissions = PortalUtil.getGuestPermissions(
498                            portletRequest, className);
499    
500                    if (groupPermissions != null) {
501                            serviceContext.setGroupPermissions(groupPermissions);
502                    }
503    
504                    if (guestPermissions != null) {
505                            serviceContext.setGuestPermissions(guestPermissions);
506                    }
507    
508                    // Expando
509    
510                    Map<String, Serializable> expandoBridgeAttributes =
511                            PortalUtil.getExpandoBridgeAttributes(
512                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
513                                            serviceContext.getCompanyId(), className),
514                                    portletRequest);
515    
516                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
517    
518                    return serviceContext;
519            }
520    
521            public static ServiceContext getInstance(
522                            String className, UploadPortletRequest uploadPortletRequest)
523                    throws PortalException {
524    
525                    ServiceContext serviceContext = getInstance(uploadPortletRequest);
526    
527                    // Permissions
528    
529                    String[] groupPermissions = PortalUtil.getGroupPermissions(
530                            uploadPortletRequest, className);
531                    String[] guestPermissions = PortalUtil.getGuestPermissions(
532                            uploadPortletRequest, className);
533    
534                    if (groupPermissions != null) {
535                            serviceContext.setGroupPermissions(groupPermissions);
536                    }
537    
538                    if (guestPermissions != null) {
539                            serviceContext.setGuestPermissions(guestPermissions);
540                    }
541    
542                    // Expando
543    
544                    Map<String, Serializable> expandoBridgeAttributes =
545                            PortalUtil.getExpandoBridgeAttributes(
546                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
547                                            serviceContext.getCompanyId(), className),
548                                    uploadPortletRequest);
549    
550                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
551    
552                    return serviceContext;
553            }
554    
555    }