001    /**
002     * Copyright (c) 2000-2012 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()));
071                            serviceContext.setLayoutURL(
072                                    PortalUtil.getCanonicalURL(
073                                            PortalUtil.getLayoutURL(themeDisplay), themeDisplay,
074                                            themeDisplay.getLayout()));
075                            serviceContext.setPathMain(PortalUtil.getPathMain());
076                            serviceContext.setPlid(themeDisplay.getPlid());
077                            serviceContext.setPortalURL(PortalUtil.getPortalURL(request));
078                            serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
079                            serviceContext.setSignedIn(themeDisplay.isSignedIn());
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                            serviceContext.setPathMain(PortalUtil.getPathMain());
092    
093                            User user = null;
094    
095                            try {
096                                    user = PortalUtil.getUser(request);
097                            }
098                            catch (NoSuchUserException nsue) {
099    
100                                    // LPS-24160
101    
102                            }
103    
104                            if (user != null) {
105                                    serviceContext.setSignedIn(!user.isDefaultUser());
106                                    serviceContext.setUserId(user.getUserId());
107                            }
108                            else {
109                                    serviceContext.setSignedIn(false);
110                            }
111                    }
112    
113                    // Attributes
114    
115                    Map<String, Serializable> attributes =
116                            new HashMap<String, Serializable>();
117    
118                    Map<String, String[]> parameters = request.getParameterMap();
119    
120                    for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
121                            String name = entry.getKey();
122                            String[] values = entry.getValue();
123    
124                            if ((values != null) && (values.length > 0)) {
125                                    if (values.length == 1) {
126                                            attributes.put(name, values[0]);
127                                    }
128                                    else {
129                                            attributes.put(name, values);
130                                    }
131                            }
132                    }
133    
134                    serviceContext.setAttributes(attributes);
135    
136                    // Command
137    
138                    String cmd = ParamUtil.getString(request, Constants.CMD);
139    
140                    serviceContext.setCommand(cmd);
141    
142                    // Current URL
143    
144                    String currentURL = PortalUtil.getCurrentURL(request);
145    
146                    serviceContext.setCurrentURL(currentURL);
147    
148                    // Form date
149    
150                    long formDateLong = ParamUtil.getLong(request, "formDate");
151    
152                    if (formDateLong > 0) {
153                            Date formDate = new Date(formDateLong);
154    
155                            serviceContext.setFormDate(formDate);
156                    }
157    
158                    // Permissions
159    
160                    boolean addGroupPermissions = ParamUtil.getBoolean(
161                            request, "addGroupPermissions");
162                    boolean addGuestPermissions = ParamUtil.getBoolean(
163                            request, "addGuestPermissions");
164                    String[] groupPermissions = PortalUtil.getGroupPermissions(request);
165                    String[] guestPermissions = PortalUtil.getGuestPermissions(request);
166    
167                    serviceContext.setAddGroupPermissions(addGroupPermissions);
168                    serviceContext.setAddGuestPermissions(addGuestPermissions);
169                    serviceContext.setGroupPermissions(groupPermissions);
170                    serviceContext.setGuestPermissions(guestPermissions);
171    
172                    // Portlet preferences ids
173    
174                    String portletId = PortalUtil.getPortletId(request);
175    
176                    if (Validator.isNotNull(portletId)) {
177                            PortletPreferencesIds portletPreferencesIds =
178                                    PortletPreferencesFactoryUtil.getPortletPreferencesIds(
179                                            request, portletId);
180    
181                            serviceContext.setPortletPreferencesIds(portletPreferencesIds);
182                    }
183    
184                    // Request
185    
186                    Map<String, String> headerMap = new HashMap<String, String>();
187    
188                    Enumeration<String> enu = request.getHeaderNames();
189    
190                    while (enu.hasMoreElements()) {
191                            String header = enu.nextElement();
192    
193                            String value = request.getHeader(header);
194    
195                            headerMap.put(header, value);
196                    }
197    
198                    serviceContext.setHeaders(headerMap);
199    
200                    serviceContext.setRemoteAddr(request.getRemoteAddr());
201                    serviceContext.setRemoteHost(request.getRemoteHost());
202                    serviceContext.setRequest(request);
203    
204                    // Asset
205    
206                    Map<String, String[]> parameterMap = request.getParameterMap();
207    
208                    List<Long> assetCategoryIdsList = new ArrayList<Long>();
209    
210                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
211                            String name = entry.getKey();
212    
213                            if (name.startsWith("assetCategoryIds")) {
214                                    long[] assetVocabularyAssetCategoryIds = StringUtil.split(
215                                            ParamUtil.getString(request, name), 0L);
216    
217                                    for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
218                                            assetCategoryIdsList.add(assetCategoryId);
219                                    }
220                            }
221                    }
222    
223                    long[] assetCategoryIds = ArrayUtil.toArray(
224                            assetCategoryIdsList.toArray(
225                                    new Long[assetCategoryIdsList.size()]));
226                    boolean assetEntryVisible = ParamUtil.getBoolean(
227                            request, "assetEntryVisible", true);
228                    long[] assetLinkEntryIds = StringUtil.split(
229                            ParamUtil.getString(
230                                    request, "assetLinkSearchContainerPrimaryKeys"), 0L);
231                    String[] assetTagNames = StringUtil.split(
232                            ParamUtil.getString(request, "assetTagNames"));
233    
234                    serviceContext.setAssetCategoryIds(assetCategoryIds);
235                    serviceContext.setAssetEntryVisible(assetEntryVisible);
236                    serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
237                    serviceContext.setAssetTagNames(assetTagNames);
238    
239                    // Workflow
240    
241                    int workflowAction = ParamUtil.getInteger(
242                            request, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
243    
244                    serviceContext.setWorkflowAction(workflowAction);
245    
246                    return serviceContext;
247            }
248    
249            public static ServiceContext getInstance(PortletRequest portletRequest)
250                    throws PortalException, SystemException {
251    
252                    // Theme display
253    
254                    ServiceContext serviceContext =
255                            ServiceContextThreadLocal.getServiceContext();
256    
257                    ThemeDisplay themeDisplay =
258                            (ThemeDisplay)portletRequest.getAttribute(WebKeys.THEME_DISPLAY);
259    
260                    if (serviceContext != null) {
261                            serviceContext = (ServiceContext)serviceContext.clone();
262                    }
263                    else {
264                            serviceContext = new ServiceContext();
265    
266                            serviceContext.setCompanyId(themeDisplay.getCompanyId());
267                            serviceContext.setLanguageId(themeDisplay.getLanguageId());
268                            serviceContext.setLayoutFullURL(
269                                    PortalUtil.getLayoutFullURL(themeDisplay));
270                            serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
271                            serviceContext.setPathMain(PortalUtil.getPathMain());
272                            serviceContext.setPlid(themeDisplay.getPlid());
273                            serviceContext.setPortalURL(
274                                    PortalUtil.getPortalURL(portletRequest));
275                            serviceContext.setSignedIn(themeDisplay.isSignedIn());
276    
277                            User user = themeDisplay.getUser();
278    
279                            serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
280                            serviceContext.setUserId(user.getUserId());
281                    }
282    
283                    serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
284    
285                    // Attributes
286    
287                    Map<String, Serializable> attributes =
288                            new HashMap<String, Serializable>();
289    
290                    Enumeration<String> enu = portletRequest.getParameterNames();
291    
292                    while (enu.hasMoreElements()) {
293                            String param = enu.nextElement();
294    
295                            String[] values = portletRequest.getParameterValues(param);
296    
297                            if ((values != null) && (values.length > 0)) {
298                                    if (values.length == 1) {
299                                            attributes.put(param, values[0]);
300                                    }
301                                    else {
302                                            attributes.put(param, values);
303                                    }
304                            }
305                    }
306    
307                    serviceContext.setAttributes(attributes);
308    
309                    // Command
310    
311                    String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
312    
313                    serviceContext.setCommand(cmd);
314    
315                    // Current URL
316    
317                    String currentURL = PortalUtil.getCurrentURL(portletRequest);
318    
319                    serviceContext.setCurrentURL(currentURL);
320    
321                    // Form date
322    
323                    long formDateLong = ParamUtil.getLong(portletRequest, "formDate");
324    
325                    if (formDateLong > 0) {
326                            Date formDate = new Date(formDateLong);
327    
328                            serviceContext.setFormDate(formDate);
329                    }
330    
331                    // Permissions
332    
333                    boolean addGroupPermissions = ParamUtil.getBoolean(
334                            portletRequest, "addGroupPermissions");
335                    boolean addGuestPermissions = ParamUtil.getBoolean(
336                            portletRequest, "addGuestPermissions");
337                    String[] groupPermissions = PortalUtil.getGroupPermissions(
338                            portletRequest);
339                    String[] guestPermissions = PortalUtil.getGuestPermissions(
340                            portletRequest);
341    
342                    serviceContext.setAddGroupPermissions(addGroupPermissions);
343                    serviceContext.setAddGuestPermissions(addGuestPermissions);
344                    serviceContext.setGroupPermissions(groupPermissions);
345                    serviceContext.setGuestPermissions(guestPermissions);
346    
347                    // Portlet preferences ids
348    
349                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
350                            portletRequest);
351    
352                    String portletId = PortalUtil.getPortletId(portletRequest);
353    
354                    PortletPreferencesIds portletPreferencesIds =
355                            PortletPreferencesFactoryUtil.getPortletPreferencesIds(
356                                    request, portletId);
357    
358                    serviceContext.setPortletPreferencesIds(portletPreferencesIds);
359    
360                    // Request
361    
362                    Map<String, String> headerMap = new HashMap<String, String>();
363    
364                    enu = request.getHeaderNames();
365    
366                    while (enu.hasMoreElements()) {
367                            String header = enu.nextElement();
368    
369                            String value = request.getHeader(header);
370    
371                            headerMap.put(header, value);
372                    }
373    
374                    serviceContext.setHeaders(headerMap);
375    
376                    serviceContext.setRemoteAddr(request.getRemoteAddr());
377                    serviceContext.setRemoteHost(request.getRemoteHost());
378                    serviceContext.setRequest(request);
379    
380                    // Asset
381    
382                    Map<String, String[]> parameterMap = portletRequest.getParameterMap();
383    
384                    List<Long> assetCategoryIdsList = new ArrayList<Long>();
385    
386                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
387                            String name = entry.getKey();
388    
389                            if (name.startsWith("assetCategoryIds")) {
390                                    long[] assetVocabularyAssetCategoryIds = StringUtil.split(
391                                            ParamUtil.getString(portletRequest, name), 0L);
392    
393                                    for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
394                                            assetCategoryIdsList.add(assetCategoryId);
395                                    }
396                            }
397                    }
398    
399                    long[] assetCategoryIds = ArrayUtil.toArray(
400                            assetCategoryIdsList.toArray(
401                                    new Long[assetCategoryIdsList.size()]));
402                    boolean assetEntryVisible = ParamUtil.getBoolean(
403                            portletRequest, "assetEntryVisible", true);
404                    long[] assetLinkEntryIds = StringUtil.split(
405                            ParamUtil.getString(
406                                    portletRequest, "assetLinkSearchContainerPrimaryKeys"), 0L);
407                    String[] assetTagNames = StringUtil.split(
408                            ParamUtil.getString(portletRequest, "assetTagNames"));
409    
410                    serviceContext.setAssetCategoryIds(assetCategoryIds);
411                    serviceContext.setAssetEntryVisible(assetEntryVisible);
412                    serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
413                    serviceContext.setAssetTagNames(assetTagNames);
414    
415                    // Workflow
416    
417                    int workflowAction = ParamUtil.getInteger(
418                            portletRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
419    
420                    serviceContext.setWorkflowAction(workflowAction);
421    
422                    return serviceContext;
423            }
424    
425            public static ServiceContext getInstance(
426                            String className, PortletRequest portletRequest)
427                    throws PortalException, SystemException {
428    
429                    ServiceContext serviceContext = getInstance(portletRequest);
430    
431                    // Expando
432    
433                    Map<String, Serializable> expandoBridgeAttributes =
434                            PortalUtil.getExpandoBridgeAttributes(
435                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
436                                            serviceContext.getCompanyId(), className),
437                                    portletRequest);
438    
439                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
440    
441                    return serviceContext;
442            }
443    
444            public static ServiceContext getInstance(
445                            String className, UploadPortletRequest uploadPortletRequest)
446                    throws PortalException, SystemException {
447    
448                    ServiceContext serviceContext = getInstance(uploadPortletRequest);
449    
450                    // Expando
451    
452                    Map<String, Serializable> expandoBridgeAttributes =
453                            PortalUtil.getExpandoBridgeAttributes(
454                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
455                                            serviceContext.getCompanyId(), className),
456                                    uploadPortletRequest);
457    
458                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
459    
460                    return serviceContext;
461            }
462    
463    }