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