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(), true));
071                            serviceContext.setLayoutURL(
072                                    PortalUtil.getCanonicalURL(
073                                            PortalUtil.getLayoutURL(themeDisplay), themeDisplay,
074                                            themeDisplay.getLayout(), true));
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                    boolean updateAssetCategoryIds = false;
211    
212                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
213                            String name = entry.getKey();
214    
215                            if (name.startsWith("assetCategoryIds")) {
216                                    updateAssetCategoryIds = true;
217    
218                                    long[] assetVocabularyAssetCategoryIds = StringUtil.split(
219                                            ParamUtil.getString(request, name), 0L);
220    
221                                    for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
222                                            assetCategoryIdsList.add(assetCategoryId);
223                                    }
224                            }
225                    }
226    
227                    if (updateAssetCategoryIds) {
228                            long[] assetCategoryIds = ArrayUtil.toArray(
229                                    assetCategoryIdsList.toArray(
230                                            new Long[assetCategoryIdsList.size()]));
231    
232                            serviceContext.setAssetCategoryIds(assetCategoryIds);
233                    }
234    
235                    boolean assetEntryVisible = ParamUtil.getBoolean(
236                            request, "assetEntryVisible", true);
237    
238                    serviceContext.setAssetEntryVisible(assetEntryVisible);
239    
240                    long[] assetLinkEntryIds = StringUtil.split(
241                            ParamUtil.getString(
242                                    request, "assetLinksSearchContainerPrimaryKeys"), 0L);
243    
244                    serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
245    
246                    String assetTagNamesString = request.getParameter("assetTagNames");
247    
248                    if (assetTagNamesString != null) {
249                            String[] assetTagNames = StringUtil.split(assetTagNamesString);
250    
251                            serviceContext.setAssetTagNames(assetTagNames);
252                    }
253    
254                    // Workflow
255    
256                    int workflowAction = ParamUtil.getInteger(
257                            request, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
258    
259                    serviceContext.setWorkflowAction(workflowAction);
260    
261                    return serviceContext;
262            }
263    
264            public static ServiceContext getInstance(PortletRequest portletRequest)
265                    throws PortalException, SystemException {
266    
267                    // Theme display
268    
269                    ServiceContext serviceContext =
270                            ServiceContextThreadLocal.getServiceContext();
271    
272                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
273                            WebKeys.THEME_DISPLAY);
274    
275                    if (serviceContext != null) {
276                            serviceContext = (ServiceContext)serviceContext.clone();
277                    }
278                    else {
279                            serviceContext = new ServiceContext();
280    
281                            serviceContext.setCompanyId(themeDisplay.getCompanyId());
282                            serviceContext.setLanguageId(themeDisplay.getLanguageId());
283                            serviceContext.setLayoutFullURL(
284                                    PortalUtil.getLayoutFullURL(themeDisplay));
285                            serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
286                            serviceContext.setPathMain(PortalUtil.getPathMain());
287                            serviceContext.setPlid(themeDisplay.getPlid());
288                            serviceContext.setPortalURL(
289                                    PortalUtil.getPortalURL(portletRequest));
290                            serviceContext.setSignedIn(themeDisplay.isSignedIn());
291    
292                            User user = themeDisplay.getUser();
293    
294                            serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
295                            serviceContext.setUserId(user.getUserId());
296                    }
297    
298                    serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
299    
300                    // Attributes
301    
302                    Map<String, Serializable> attributes =
303                            new HashMap<String, Serializable>();
304    
305                    Enumeration<String> enu = portletRequest.getParameterNames();
306    
307                    while (enu.hasMoreElements()) {
308                            String param = enu.nextElement();
309    
310                            String[] values = portletRequest.getParameterValues(param);
311    
312                            if ((values != null) && (values.length > 0)) {
313                                    if (values.length == 1) {
314                                            attributes.put(param, values[0]);
315                                    }
316                                    else {
317                                            attributes.put(param, values);
318                                    }
319                            }
320                    }
321    
322                    serviceContext.setAttributes(attributes);
323    
324                    // Command
325    
326                    String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
327    
328                    serviceContext.setCommand(cmd);
329    
330                    // Current URL
331    
332                    String currentURL = PortalUtil.getCurrentURL(portletRequest);
333    
334                    serviceContext.setCurrentURL(currentURL);
335    
336                    // Form date
337    
338                    long formDateLong = ParamUtil.getLong(portletRequest, "formDate");
339    
340                    if (formDateLong > 0) {
341                            Date formDate = new Date(formDateLong);
342    
343                            serviceContext.setFormDate(formDate);
344                    }
345    
346                    // Permissions
347    
348                    boolean addGroupPermissions = ParamUtil.getBoolean(
349                            portletRequest, "addGroupPermissions");
350                    boolean addGuestPermissions = ParamUtil.getBoolean(
351                            portletRequest, "addGuestPermissions");
352                    String[] groupPermissions = PortalUtil.getGroupPermissions(
353                            portletRequest);
354                    String[] guestPermissions = PortalUtil.getGuestPermissions(
355                            portletRequest);
356    
357                    serviceContext.setAddGroupPermissions(addGroupPermissions);
358                    serviceContext.setAddGuestPermissions(addGuestPermissions);
359                    serviceContext.setGroupPermissions(groupPermissions);
360                    serviceContext.setGuestPermissions(guestPermissions);
361    
362                    // Portlet preferences ids
363    
364                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
365                            portletRequest);
366    
367                    String portletId = PortalUtil.getPortletId(portletRequest);
368    
369                    PortletPreferencesIds portletPreferencesIds =
370                            PortletPreferencesFactoryUtil.getPortletPreferencesIds(
371                                    request, portletId);
372    
373                    serviceContext.setPortletPreferencesIds(portletPreferencesIds);
374    
375                    // Request
376    
377                    Map<String, String> headerMap = new HashMap<String, String>();
378    
379                    enu = request.getHeaderNames();
380    
381                    while (enu.hasMoreElements()) {
382                            String header = enu.nextElement();
383    
384                            String value = request.getHeader(header);
385    
386                            headerMap.put(header, value);
387                    }
388    
389                    serviceContext.setHeaders(headerMap);
390    
391                    serviceContext.setRemoteAddr(request.getRemoteAddr());
392                    serviceContext.setRemoteHost(request.getRemoteHost());
393                    serviceContext.setRequest(request);
394    
395                    // Asset
396    
397                    Map<String, String[]> parameterMap = portletRequest.getParameterMap();
398    
399                    List<Long> assetCategoryIdsList = new ArrayList<Long>();
400    
401                    boolean updateAssetCategoryIds = false;
402    
403                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
404                            String name = entry.getKey();
405    
406                            if (name.startsWith("assetCategoryIds")) {
407                                    updateAssetCategoryIds = true;
408    
409                                    long[] assetVocabularyAssetCategoryIds = StringUtil.split(
410                                            ParamUtil.getString(portletRequest, name), 0L);
411    
412                                    for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
413                                            assetCategoryIdsList.add(assetCategoryId);
414                                    }
415                            }
416                    }
417    
418                    if (updateAssetCategoryIds) {
419                            long[] assetCategoryIds = ArrayUtil.toArray(
420                                    assetCategoryIdsList.toArray(
421                                            new Long[assetCategoryIdsList.size()]));
422    
423                            serviceContext.setAssetCategoryIds(assetCategoryIds);
424                    }
425    
426                    boolean assetEntryVisible = ParamUtil.getBoolean(
427                            portletRequest, "assetEntryVisible", true);
428    
429                    serviceContext.setAssetEntryVisible(assetEntryVisible);
430    
431                    long[] assetLinkEntryIds = StringUtil.split(
432                            ParamUtil.getString(
433                                    portletRequest, "assetLinksSearchContainerPrimaryKeys"), 0L);
434    
435                    serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
436    
437                    String assetTagNamesString = request.getParameter("assetTagNames");
438    
439                    if (assetTagNamesString != null) {
440                            String[] assetTagNames = StringUtil.split(assetTagNamesString);
441    
442                            serviceContext.setAssetTagNames(assetTagNames);
443                    }
444    
445                    // Workflow
446    
447                    int workflowAction = ParamUtil.getInteger(
448                            portletRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
449    
450                    serviceContext.setWorkflowAction(workflowAction);
451    
452                    return serviceContext;
453            }
454    
455            public static ServiceContext getInstance(
456                            String className, PortletRequest portletRequest)
457                    throws PortalException, SystemException {
458    
459                    ServiceContext serviceContext = getInstance(portletRequest);
460    
461                    // Permissions
462    
463                    String[] groupPermissions = PortalUtil.getGroupPermissions(
464                            portletRequest, className);
465                    String[] guestPermissions = PortalUtil.getGuestPermissions(
466                            portletRequest, className);
467    
468                    if (groupPermissions != null) {
469                            serviceContext.setGroupPermissions(groupPermissions);
470                    }
471    
472                    if (guestPermissions != null) {
473                            serviceContext.setGuestPermissions(guestPermissions);
474                    }
475    
476                    // Expando
477    
478                    Map<String, Serializable> expandoBridgeAttributes =
479                            PortalUtil.getExpandoBridgeAttributes(
480                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
481                                            serviceContext.getCompanyId(), className),
482                                    portletRequest);
483    
484                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
485    
486                    return serviceContext;
487            }
488    
489            public static ServiceContext getInstance(
490                            String className, UploadPortletRequest uploadPortletRequest)
491                    throws PortalException, SystemException {
492    
493                    ServiceContext serviceContext = getInstance(uploadPortletRequest);
494    
495                    // Permissions
496    
497                    String[] groupPermissions = PortalUtil.getGroupPermissions(
498                            uploadPortletRequest, className);
499                    String[] guestPermissions = PortalUtil.getGuestPermissions(
500                            uploadPortletRequest, className);
501    
502                    if (groupPermissions != null) {
503                            serviceContext.setGroupPermissions(groupPermissions);
504                    }
505    
506                    if (guestPermissions != null) {
507                            serviceContext.setGuestPermissions(guestPermissions);
508                    }
509    
510                    // Expando
511    
512                    Map<String, Serializable> expandoBridgeAttributes =
513                            PortalUtil.getExpandoBridgeAttributes(
514                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
515                                            serviceContext.getCompanyId(), className),
516                                    uploadPortletRequest);
517    
518                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
519    
520                    return serviceContext;
521            }
522    
523    }