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