001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.model.PortletPreferencesIds;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.PortletPreferencesFactoryUtil;
029    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
030    
031    import java.io.Serializable;
032    
033    import java.util.Enumeration;
034    import java.util.HashMap;
035    import java.util.Map;
036    
037    import javax.portlet.PortletRequest;
038    
039    import javax.servlet.http.HttpServletRequest;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Raymond Augé
044     */
045    public class ServiceContextFactory {
046    
047            public static ServiceContext getInstance(HttpServletRequest request)
048                    throws PortalException, SystemException {
049    
050                    ServiceContext serviceContext = new ServiceContext();
051    
052                    // Theme display
053    
054                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
055                            WebKeys.THEME_DISPLAY);
056    
057                    if (themeDisplay != null) {
058                            serviceContext.setCompanyId(themeDisplay.getCompanyId());
059                            serviceContext.setLanguageId(themeDisplay.getLanguageId());
060                            serviceContext.setLayoutFullURL(
061                                    PortalUtil.getLayoutFullURL(themeDisplay));
062                            serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
063                            serviceContext.setPathMain(PortalUtil.getPathMain());
064                            serviceContext.setPlid(themeDisplay.getPlid());
065                            serviceContext.setPortalURL(PortalUtil.getPortalURL(request));
066                            serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
067                            serviceContext.setSignedIn(themeDisplay.isSignedIn());
068    
069                            User user = themeDisplay.getUser();
070    
071                            serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
072                            serviceContext.setUserId(user.getUserId());
073                    }
074                    else {
075                            long companyId = PortalUtil.getCompanyId(request);
076    
077                            serviceContext.setCompanyId(companyId);
078    
079                            serviceContext.setPathMain(PortalUtil.getPathMain());
080    
081                            User user = PortalUtil.getUser(request);
082    
083                            if (user != null) {
084                                    serviceContext.setSignedIn(!user.isDefaultUser());
085                                    serviceContext.setUserId(user.getUserId());
086                            }
087                            else {
088                                    serviceContext.setSignedIn(false);
089                            }
090                    }
091    
092                    // Attributes
093    
094                    Map<String, Serializable> attributes =
095                            new HashMap<String, Serializable>();
096    
097                    Enumeration<String> enu = request.getParameterNames();
098    
099                    while (enu.hasMoreElements()) {
100                            String param = enu.nextElement();
101    
102                            String[] values = request.getParameterValues(param);
103    
104                            if ((values != null) && (values.length > 0)) {
105                                    if (values.length == 1) {
106                                            attributes.put(param, values[0]);
107                                    }
108                                    else {
109                                            attributes.put(param, values);
110                                    }
111                            }
112                    }
113    
114                    serviceContext.setAttributes(attributes);
115    
116                    // Request
117    
118                    Map<String, String> headerMap = new HashMap<String, String>();
119    
120                    enu = request.getHeaderNames();
121    
122                    while (enu.hasMoreElements()) {
123                            String header = enu.nextElement();
124    
125                            String value = request.getHeader(header);
126    
127                            headerMap.put(header, value);
128                    }
129    
130                    serviceContext.setHeaders(headerMap);
131    
132                    serviceContext.setRemoteAddr(request.getRemoteAddr());
133                    serviceContext.setRemoteHost(request.getRemoteHost());
134    
135                    return serviceContext;
136            }
137    
138            public static ServiceContext getInstance(PortletRequest portletRequest)
139                    throws PortalException, SystemException {
140    
141                    // Theme display
142    
143                    ServiceContext serviceContext =
144                            ServiceContextThreadLocal.getServiceContext();
145    
146                    ThemeDisplay themeDisplay =
147                            (ThemeDisplay)portletRequest.getAttribute(
148                                    WebKeys.THEME_DISPLAY);
149    
150                    if (serviceContext != null) {
151                            serviceContext = (ServiceContext)serviceContext.clone();
152                    }
153                    else {
154                            serviceContext = new ServiceContext();
155    
156                            serviceContext.setCompanyId(themeDisplay.getCompanyId());
157                            serviceContext.setLanguageId(themeDisplay.getLanguageId());
158                            serviceContext.setLayoutFullURL(
159                                    PortalUtil.getLayoutFullURL(themeDisplay));
160                            serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
161                            serviceContext.setPathMain(PortalUtil.getPathMain());
162                            serviceContext.setPlid(themeDisplay.getPlid());
163                            serviceContext.setPortalURL(
164                                    PortalUtil.getPortalURL(portletRequest));
165                            serviceContext.setSignedIn(themeDisplay.isSignedIn());
166    
167                            User user = themeDisplay.getUser();
168    
169                            serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
170                            serviceContext.setUserId(user.getUserId());
171                    }
172    
173                    serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
174    
175                    // Attributes
176    
177                    Map<String, Serializable> attributes =
178                            new HashMap<String, Serializable>();
179    
180                    Enumeration<String> enu = portletRequest.getParameterNames();
181    
182                    while (enu.hasMoreElements()) {
183                            String param = enu.nextElement();
184    
185                            String[] values = portletRequest.getParameterValues(param);
186    
187                            if ((values != null) && (values.length > 0)) {
188                                    if (values.length == 1) {
189                                            attributes.put(param, values[0]);
190                                    }
191                                    else {
192                                            attributes.put(param, values);
193                                    }
194                            }
195                    }
196    
197                    serviceContext.setAttributes(attributes);
198    
199                    // Command
200    
201                    String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
202    
203                    serviceContext.setCommand(cmd);
204    
205                    // Current URL
206    
207                    String currentURL = PortalUtil.getCurrentURL(portletRequest);
208    
209                    serviceContext.setCurrentURL(currentURL);
210    
211                    // Permissions
212    
213                    boolean addCommunityPermissions = ParamUtil.getBoolean(
214                            portletRequest, "addCommunityPermissions");
215                    boolean addGuestPermissions = ParamUtil.getBoolean(
216                            portletRequest, "addGuestPermissions");
217                    String[] communityPermissions = PortalUtil.getCommunityPermissions(
218                            portletRequest);
219                    String[] guestPermissions = PortalUtil.getGuestPermissions(
220                            portletRequest);
221    
222                    serviceContext.setAddCommunityPermissions(addCommunityPermissions);
223                    serviceContext.setAddGuestPermissions(addGuestPermissions);
224                    serviceContext.setCommunityPermissions(communityPermissions);
225                    serviceContext.setGuestPermissions(guestPermissions);
226    
227                    // Portlet preferences ids
228    
229                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
230                            portletRequest);
231    
232                    String portletId = PortalUtil.getPortletId(portletRequest);
233    
234                    PortletPreferencesIds portletPreferencesIds =
235                            PortletPreferencesFactoryUtil.getPortletPreferencesIds(
236                                    request, portletId);
237    
238                    serviceContext.setPortletPreferencesIds(portletPreferencesIds);
239    
240                    // Request
241    
242                    Map<String, String> headerMap = new HashMap<String, String>();
243    
244                    enu = request.getHeaderNames();
245    
246                    while (enu.hasMoreElements()) {
247                            String header = enu.nextElement();
248    
249                            String value = request.getHeader(header);
250    
251                            headerMap.put(header, value);
252                    }
253    
254                    serviceContext.setHeaders(headerMap);
255    
256                    serviceContext.setRemoteAddr(request.getRemoteAddr());
257                    serviceContext.setRemoteHost(request.getRemoteHost());
258    
259                    // Asset
260    
261                    long[] assetCategoryIds = StringUtil.split(
262                            ParamUtil.getString(portletRequest, "assetCategoryIds"), 0L);
263                    String[] assetTagNames = StringUtil.split(
264                            ParamUtil.getString(portletRequest, "assetTagNames"));
265    
266                    serviceContext.setAssetCategoryIds(assetCategoryIds);
267                    serviceContext.setAssetTagNames(assetTagNames);
268    
269                    // Workflow
270    
271                    int workflowAction = ParamUtil.getInteger(
272                            portletRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
273    
274                    serviceContext.setWorkflowAction(workflowAction);
275    
276                    return serviceContext;
277            }
278    
279            public static ServiceContext getInstance(
280                            String className, PortletRequest portletRequest)
281                    throws PortalException, SystemException {
282    
283                    ServiceContext serviceContext = getInstance(portletRequest);
284    
285                    // Expando
286    
287                    Map<String, Serializable> expandoBridgeAttributes =
288                            PortalUtil.getExpandoBridgeAttributes(
289                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
290                                            serviceContext.getCompanyId(), className),
291                                    portletRequest);
292    
293                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
294    
295                    return serviceContext;
296            }
297    
298    }