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.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.PortletPreferencesIds;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.PortletPreferencesFactoryUtil;
032    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
033    
034    import java.io.Serializable;
035    
036    import java.util.ArrayList;
037    import java.util.Enumeration;
038    import java.util.HashMap;
039    import java.util.List;
040    import java.util.Map;
041    
042    import javax.portlet.PortletRequest;
043    
044    import javax.servlet.http.HttpServletRequest;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     * @author Raymond Augé
049     */
050    public class ServiceContextFactory {
051    
052            public static ServiceContext getInstance(HttpServletRequest request)
053                    throws PortalException, SystemException {
054    
055                    ServiceContext serviceContext = new ServiceContext();
056    
057                    // Theme display
058    
059                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
060                            WebKeys.THEME_DISPLAY);
061    
062                    if (themeDisplay != null) {
063                            serviceContext.setCompanyId(themeDisplay.getCompanyId());
064                            serviceContext.setLanguageId(themeDisplay.getLanguageId());
065                            serviceContext.setLayoutFullURL(
066                                    PortalUtil.getCanonicalURL(
067                                            PortalUtil.getLayoutFullURL(themeDisplay), themeDisplay));
068                            serviceContext.setLayoutURL(
069                                    PortalUtil.getCanonicalURL(
070                                            PortalUtil.getLayoutURL(themeDisplay), themeDisplay));
071                            serviceContext.setPathMain(PortalUtil.getPathMain());
072                            serviceContext.setPlid(themeDisplay.getPlid());
073                            serviceContext.setPortalURL(
074                                    PortalUtil.getCanonicalURL(
075                                            PortalUtil.getPortalURL(request), themeDisplay));
076                            serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
077                            serviceContext.setSignedIn(themeDisplay.isSignedIn());
078    
079                            User user = themeDisplay.getUser();
080    
081                            serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
082                            serviceContext.setUserId(user.getUserId());
083                    }
084                    else {
085                            long companyId = PortalUtil.getCompanyId(request);
086    
087                            serviceContext.setCompanyId(companyId);
088    
089                            serviceContext.setPathMain(PortalUtil.getPathMain());
090    
091                            User user = null;
092    
093                            try {
094                                    user = PortalUtil.getUser(request);
095                            }
096                            catch (NoSuchUserException nsue) {
097    
098                                    // LPS-24160
099    
100                            }
101    
102                            if (user != null) {
103                                    serviceContext.setSignedIn(!user.isDefaultUser());
104                                    serviceContext.setUserId(user.getUserId());
105                            }
106                            else {
107                                    serviceContext.setSignedIn(false);
108                            }
109                    }
110    
111                    // Attributes
112    
113                    Map<String, Serializable> attributes =
114                            new HashMap<String, Serializable>();
115    
116                    Map<String, String[]> parameters = request.getParameterMap();
117    
118                    for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
119                            String name = entry.getKey();
120                            String[] values = entry.getValue();
121    
122                            if ((values != null) && (values.length > 0)) {
123                                    if (values.length == 1) {
124                                            attributes.put(name, values[0]);
125                                    }
126                                    else {
127                                            attributes.put(name, values);
128                                    }
129                            }
130                    }
131    
132                    serviceContext.setAttributes(attributes);
133    
134                    // Command
135    
136                    String cmd = ParamUtil.getString(request, Constants.CMD);
137    
138                    serviceContext.setCommand(cmd);
139    
140                    // Current URL
141    
142                    String currentURL = PortalUtil.getCurrentURL(request);
143    
144                    serviceContext.setCurrentURL(currentURL);
145    
146                    // Permissions
147    
148                    boolean addGroupPermissions = ParamUtil.getBoolean(
149                            request, "addGroupPermissions");
150                    boolean addGuestPermissions = ParamUtil.getBoolean(
151                            request, "addGuestPermissions");
152                    String[] groupPermissions = PortalUtil.getGroupPermissions(request);
153                    String[] guestPermissions = PortalUtil.getGuestPermissions(request);
154    
155                    serviceContext.setAddGroupPermissions(addGroupPermissions);
156                    serviceContext.setAddGuestPermissions(addGuestPermissions);
157                    serviceContext.setGroupPermissions(groupPermissions);
158                    serviceContext.setGuestPermissions(guestPermissions);
159    
160                    // Portlet preferences ids
161    
162                    String portletId = PortalUtil.getPortletId(request);
163    
164                    if (Validator.isNotNull(portletId)) {
165                            PortletPreferencesIds portletPreferencesIds =
166                                    PortletPreferencesFactoryUtil.getPortletPreferencesIds(
167                                            request, portletId);
168    
169                            serviceContext.setPortletPreferencesIds(portletPreferencesIds);
170                    }
171    
172                    // Request
173    
174                    Map<String, String> headerMap = new HashMap<String, String>();
175    
176                    Enumeration<String> enu = request.getHeaderNames();
177    
178                    while (enu.hasMoreElements()) {
179                            String header = enu.nextElement();
180    
181                            String value = request.getHeader(header);
182    
183                            headerMap.put(header, value);
184                    }
185    
186                    serviceContext.setHeaders(headerMap);
187    
188                    serviceContext.setRemoteAddr(request.getRemoteAddr());
189                    serviceContext.setRemoteHost(request.getRemoteHost());
190    
191                    // Asset
192    
193                    Map<String, String[]> parameterMap = request.getParameterMap();
194    
195                    List<Long> assetCategoryIdsList = new ArrayList<Long>();
196    
197                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
198                            String name = entry.getKey();
199    
200                            if (name.startsWith("assetCategoryIds")) {
201                                    long[] assetVocabularyAssetCategoryIds = StringUtil.split(
202                                            ParamUtil.getString(request, name), 0L);
203    
204                                    for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
205                                            assetCategoryIdsList.add(assetCategoryId);
206                                    }
207                            }
208                    }
209    
210                    long[] assetCategoryIds = ArrayUtil.toArray(
211                            assetCategoryIdsList.toArray(
212                                    new Long[assetCategoryIdsList.size()]));
213                    boolean assetEntryVisible = ParamUtil.getBoolean(
214                            request, "assetEntryVisible", true);
215                    long[] assetLinkEntryIds = StringUtil.split(
216                            ParamUtil.getString(
217                                    request, "assetLinkSearchContainerPrimaryKeys"), 0L);
218                    String[] assetTagNames = StringUtil.split(
219                            ParamUtil.getString(request, "assetTagNames"));
220    
221                    serviceContext.setAssetCategoryIds(assetCategoryIds);
222                    serviceContext.setAssetEntryVisible(assetEntryVisible);
223                    serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
224                    serviceContext.setAssetTagNames(assetTagNames);
225    
226                    // Workflow
227    
228                    int workflowAction = ParamUtil.getInteger(
229                            request, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
230    
231                    serviceContext.setWorkflowAction(workflowAction);
232    
233                    return serviceContext;
234            }
235    
236            public static ServiceContext getInstance(PortletRequest portletRequest)
237                    throws PortalException, SystemException {
238    
239                    // Theme display
240    
241                    ServiceContext serviceContext =
242                            ServiceContextThreadLocal.getServiceContext();
243    
244                    ThemeDisplay themeDisplay =
245                            (ThemeDisplay)portletRequest.getAttribute(WebKeys.THEME_DISPLAY);
246    
247                    if (serviceContext != null) {
248                            serviceContext = (ServiceContext)serviceContext.clone();
249                    }
250                    else {
251                            serviceContext = new ServiceContext();
252    
253                            serviceContext.setCompanyId(themeDisplay.getCompanyId());
254                            serviceContext.setLanguageId(themeDisplay.getLanguageId());
255                            serviceContext.setLayoutFullURL(
256                                    PortalUtil.getLayoutFullURL(themeDisplay));
257                            serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
258                            serviceContext.setPathMain(PortalUtil.getPathMain());
259                            serviceContext.setPlid(themeDisplay.getPlid());
260                            serviceContext.setPortalURL(
261                                    PortalUtil.getPortalURL(portletRequest));
262                            serviceContext.setSignedIn(themeDisplay.isSignedIn());
263    
264                            User user = themeDisplay.getUser();
265    
266                            serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
267                            serviceContext.setUserId(user.getUserId());
268                    }
269    
270                    serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
271    
272                    // Attributes
273    
274                    Map<String, Serializable> attributes =
275                            new HashMap<String, Serializable>();
276    
277                    Enumeration<String> enu = portletRequest.getParameterNames();
278    
279                    while (enu.hasMoreElements()) {
280                            String param = enu.nextElement();
281    
282                            String[] values = portletRequest.getParameterValues(param);
283    
284                            if ((values != null) && (values.length > 0)) {
285                                    if (values.length == 1) {
286                                            attributes.put(param, values[0]);
287                                    }
288                                    else {
289                                            attributes.put(param, values);
290                                    }
291                            }
292                    }
293    
294                    serviceContext.setAttributes(attributes);
295    
296                    // Command
297    
298                    String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
299    
300                    serviceContext.setCommand(cmd);
301    
302                    // Current URL
303    
304                    String currentURL = PortalUtil.getCurrentURL(portletRequest);
305    
306                    serviceContext.setCurrentURL(currentURL);
307    
308                    // Permissions
309    
310                    boolean addGroupPermissions = ParamUtil.getBoolean(
311                            portletRequest, "addGroupPermissions");
312                    boolean addGuestPermissions = ParamUtil.getBoolean(
313                            portletRequest, "addGuestPermissions");
314                    String[] groupPermissions = PortalUtil.getGroupPermissions(
315                            portletRequest);
316                    String[] guestPermissions = PortalUtil.getGuestPermissions(
317                            portletRequest);
318    
319                    serviceContext.setAddGroupPermissions(addGroupPermissions);
320                    serviceContext.setAddGuestPermissions(addGuestPermissions);
321                    serviceContext.setGroupPermissions(groupPermissions);
322                    serviceContext.setGuestPermissions(guestPermissions);
323    
324                    // Portlet preferences ids
325    
326                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
327                            portletRequest);
328    
329                    String portletId = PortalUtil.getPortletId(portletRequest);
330    
331                    PortletPreferencesIds portletPreferencesIds =
332                            PortletPreferencesFactoryUtil.getPortletPreferencesIds(
333                                    request, portletId);
334    
335                    serviceContext.setPortletPreferencesIds(portletPreferencesIds);
336    
337                    // Request
338    
339                    Map<String, String> headerMap = new HashMap<String, String>();
340    
341                    enu = request.getHeaderNames();
342    
343                    while (enu.hasMoreElements()) {
344                            String header = enu.nextElement();
345    
346                            String value = request.getHeader(header);
347    
348                            headerMap.put(header, value);
349                    }
350    
351                    serviceContext.setHeaders(headerMap);
352    
353                    serviceContext.setRemoteAddr(request.getRemoteAddr());
354                    serviceContext.setRemoteHost(request.getRemoteHost());
355    
356                    // Asset
357    
358                    Map<String, String[]> parameterMap = portletRequest.getParameterMap();
359    
360                    List<Long> assetCategoryIdsList = new ArrayList<Long>();
361    
362                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
363                            String name = entry.getKey();
364    
365                            if (name.startsWith("assetCategoryIds")) {
366                                    long[] assetVocabularyAssetCategoryIds = StringUtil.split(
367                                            ParamUtil.getString(portletRequest, name), 0L);
368    
369                                    for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
370                                            assetCategoryIdsList.add(assetCategoryId);
371                                    }
372                            }
373                    }
374    
375                    long[] assetCategoryIds = ArrayUtil.toArray(
376                            assetCategoryIdsList.toArray(
377                                    new Long[assetCategoryIdsList.size()]));
378                    boolean assetEntryVisible = ParamUtil.getBoolean(
379                            portletRequest, "assetEntryVisible", true);
380                    long[] assetLinkEntryIds = StringUtil.split(
381                            ParamUtil.getString(
382                                    portletRequest, "assetLinkSearchContainerPrimaryKeys"), 0L);
383                    String[] assetTagNames = StringUtil.split(
384                            ParamUtil.getString(portletRequest, "assetTagNames"));
385    
386                    serviceContext.setAssetCategoryIds(assetCategoryIds);
387                    serviceContext.setAssetEntryVisible(assetEntryVisible);
388                    serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
389                    serviceContext.setAssetTagNames(assetTagNames);
390    
391                    // Workflow
392    
393                    int workflowAction = ParamUtil.getInteger(
394                            portletRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
395    
396                    serviceContext.setWorkflowAction(workflowAction);
397    
398                    return serviceContext;
399            }
400    
401            public static ServiceContext getInstance(
402                            String className, PortletRequest portletRequest)
403                    throws PortalException, SystemException {
404    
405                    ServiceContext serviceContext = getInstance(portletRequest);
406    
407                    // Expando
408    
409                    Map<String, Serializable> expandoBridgeAttributes =
410                            PortalUtil.getExpandoBridgeAttributes(
411                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
412                                            serviceContext.getCompanyId(), className),
413                                    portletRequest);
414    
415                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
416    
417                    return serviceContext;
418            }
419    
420    }