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