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