001
014
015 package com.liferay.portal.service;
016
017 import com.liferay.portal.exception.NoSuchUserException;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.upload.UploadPortletRequest;
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.Group;
028 import com.liferay.portal.model.GroupConstants;
029 import com.liferay.portal.model.PortletPreferencesIds;
030 import com.liferay.portal.model.User;
031 import com.liferay.portal.service.permission.ModelPermissions;
032 import com.liferay.portal.service.permission.ModelPermissionsFactory;
033 import com.liferay.portal.theme.ThemeDisplay;
034 import com.liferay.portal.util.PortalUtil;
035 import com.liferay.portlet.PortletPreferencesFactoryUtil;
036 import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
037
038 import java.io.Serializable;
039
040 import java.util.ArrayList;
041 import java.util.Date;
042 import java.util.Enumeration;
043 import java.util.HashMap;
044 import java.util.List;
045 import java.util.Map;
046
047 import javax.portlet.PortletRequest;
048
049 import javax.servlet.http.HttpServletRequest;
050
051
055 public class ServiceContextFactory {
056
057 public static ServiceContext getInstance(HttpServletRequest request)
058 throws PortalException {
059
060 ServiceContext serviceContext = new ServiceContext();
061
062
063
064 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
065 WebKeys.THEME_DISPLAY);
066
067 if (themeDisplay != null) {
068 serviceContext.setCompanyId(themeDisplay.getCompanyId());
069 serviceContext.setLanguageId(themeDisplay.getLanguageId());
070 serviceContext.setLayoutFullURL(
071 PortalUtil.getCanonicalURL(
072 PortalUtil.getLayoutFullURL(themeDisplay), themeDisplay,
073 themeDisplay.getLayout(), true));
074 serviceContext.setLayoutURL(
075 PortalUtil.getCanonicalURL(
076 PortalUtil.getLayoutURL(themeDisplay), themeDisplay,
077 themeDisplay.getLayout(), true));
078 serviceContext.setPlid(themeDisplay.getPlid());
079 serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
080 serviceContext.setSignedIn(themeDisplay.isSignedIn());
081 serviceContext.setTimeZone(themeDisplay.getTimeZone());
082
083 User user = themeDisplay.getUser();
084
085 serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
086 serviceContext.setUserId(user.getUserId());
087 }
088 else {
089 long companyId = PortalUtil.getCompanyId(request);
090
091 serviceContext.setCompanyId(companyId);
092
093 Group guestGroup = GroupLocalServiceUtil.getGroup(
094 serviceContext.getCompanyId(), GroupConstants.GUEST);
095
096 serviceContext.setScopeGroupId(guestGroup.getGroupId());
097
098 long plid = LayoutLocalServiceUtil.getDefaultPlid(
099 serviceContext.getScopeGroupId(), false);
100
101 serviceContext.setPlid(plid);
102
103 User user = null;
104
105 try {
106 user = PortalUtil.getUser(request);
107 }
108 catch (NoSuchUserException nsue) {
109
110
111
112 }
113
114 if (user != null) {
115 serviceContext.setSignedIn(!user.isDefaultUser());
116 serviceContext.setUserId(user.getUserId());
117 }
118 else {
119 serviceContext.setSignedIn(false);
120 }
121 }
122
123 serviceContext.setPortalURL(PortalUtil.getPortalURL(request));
124 serviceContext.setPathMain(PortalUtil.getPathMain());
125 serviceContext.setPathFriendlyURLPrivateGroup(
126 PortalUtil.getPathFriendlyURLPrivateGroup());
127 serviceContext.setPathFriendlyURLPrivateUser(
128 PortalUtil.getPathFriendlyURLPrivateUser());
129 serviceContext.setPathFriendlyURLPublic(
130 PortalUtil.getPathFriendlyURLPublic());
131
132
133
134 Map<String, Serializable> attributes = new HashMap<>();
135
136 Map<String, String[]> parameters = request.getParameterMap();
137
138 for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
139 String name = entry.getKey();
140 String[] values = entry.getValue();
141
142 if (ArrayUtil.isNotEmpty(values)) {
143 if (values.length == 1) {
144 attributes.put(name, values[0]);
145 }
146 else {
147 attributes.put(name, values);
148 }
149 }
150 }
151
152 serviceContext.setAttributes(attributes);
153
154
155
156 String cmd = ParamUtil.getString(request, Constants.CMD);
157
158 serviceContext.setCommand(cmd);
159
160
161
162 String currentURL = PortalUtil.getCurrentURL(request);
163
164 serviceContext.setCurrentURL(currentURL);
165
166
167
168 long formDateLong = ParamUtil.getLong(request, "formDate");
169
170 if (formDateLong > 0) {
171 Date formDate = new Date(formDateLong);
172
173 serviceContext.setFormDate(formDate);
174 }
175
176
177
178 ModelPermissions modelPermissions = ModelPermissionsFactory.create(
179 request);
180
181 if (!modelPermissions.isEmpty()) {
182 serviceContext.setModelPermissions(modelPermissions);
183 }
184 else {
185 boolean addGroupPermissions = ParamUtil.getBoolean(
186 request, "addGroupPermissions");
187 boolean addGuestPermissions = ParamUtil.getBoolean(
188 request, "addGuestPermissions");
189 String[] groupPermissions = PortalUtil.getGroupPermissions(request);
190 String[] guestPermissions = PortalUtil.getGuestPermissions(request);
191
192 serviceContext.setAddGroupPermissions(addGroupPermissions);
193 serviceContext.setAddGuestPermissions(addGuestPermissions);
194 serviceContext.setGroupPermissions(groupPermissions);
195 serviceContext.setGuestPermissions(guestPermissions);
196 }
197
198
199
200 String portletId = PortalUtil.getPortletId(request);
201
202 if (Validator.isNotNull(portletId)) {
203 PortletPreferencesIds portletPreferencesIds =
204 PortletPreferencesFactoryUtil.getPortletPreferencesIds(
205 request, portletId);
206
207 serviceContext.setPortletPreferencesIds(portletPreferencesIds);
208 }
209
210
211
212 Map<String, String> headerMap = new HashMap<>();
213
214 Enumeration<String> enu = request.getHeaderNames();
215
216 while (enu.hasMoreElements()) {
217 String header = enu.nextElement();
218
219 String value = request.getHeader(header);
220
221 headerMap.put(header, value);
222 }
223
224 serviceContext.setHeaders(headerMap);
225
226 serviceContext.setRemoteAddr(request.getRemoteAddr());
227 serviceContext.setRemoteHost(request.getRemoteHost());
228 serviceContext.setRequest(request);
229
230
231
232 Map<String, String[]> parameterMap = request.getParameterMap();
233
234 List<Long> assetCategoryIdsList = new ArrayList<>();
235
236 boolean updateAssetCategoryIds = false;
237
238 for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
239 String name = entry.getKey();
240
241 if (name.startsWith("assetCategoryIds")) {
242 updateAssetCategoryIds = true;
243
244 long[] assetVocabularyAssetCategoryIds = StringUtil.split(
245 ParamUtil.getString(request, name), 0L);
246
247 for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
248 assetCategoryIdsList.add(assetCategoryId);
249 }
250 }
251 }
252
253 if (updateAssetCategoryIds) {
254 long[] assetCategoryIds = ArrayUtil.toArray(
255 assetCategoryIdsList.toArray(
256 new Long[assetCategoryIdsList.size()]));
257
258 serviceContext.setAssetCategoryIds(assetCategoryIds);
259 }
260
261 boolean assetEntryVisible = ParamUtil.getBoolean(
262 request, "assetEntryVisible", true);
263
264 serviceContext.setAssetEntryVisible(assetEntryVisible);
265
266 String assetLinkEntryIdsString = request.getParameter(
267 "assetLinksSearchContainerPrimaryKeys");
268
269 if (assetLinkEntryIdsString != null) {
270 long[] assetLinkEntryIds = StringUtil.split(
271 assetLinkEntryIdsString, 0L);
272
273 serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
274 }
275
276 Double assetPriority = ParamUtil.getDouble(request, "assetPriority");
277
278 serviceContext.setAssetPriority(assetPriority);
279
280 String assetTagNamesString = request.getParameter("assetTagNames");
281
282 if (assetTagNamesString != null) {
283 String[] assetTagNames = StringUtil.split(assetTagNamesString);
284
285 serviceContext.setAssetTagNames(assetTagNames);
286 }
287
288
289
290 int workflowAction = ParamUtil.getInteger(
291 request, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
292
293 serviceContext.setWorkflowAction(workflowAction);
294
295 return serviceContext;
296 }
297
298 public static ServiceContext getInstance(PortletRequest portletRequest)
299 throws PortalException {
300
301
302
303 ServiceContext serviceContext =
304 ServiceContextThreadLocal.getServiceContext();
305
306 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
307 WebKeys.THEME_DISPLAY);
308
309 if (serviceContext != null) {
310 serviceContext = (ServiceContext)serviceContext.clone();
311 }
312 else {
313 serviceContext = new ServiceContext();
314
315 serviceContext.setCompanyId(themeDisplay.getCompanyId());
316 serviceContext.setLanguageId(themeDisplay.getLanguageId());
317 serviceContext.setLayoutFullURL(
318 PortalUtil.getLayoutFullURL(themeDisplay));
319 serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
320 serviceContext.setPathFriendlyURLPrivateGroup(
321 PortalUtil.getPathFriendlyURLPrivateGroup());
322 serviceContext.setPathFriendlyURLPrivateUser(
323 PortalUtil.getPathFriendlyURLPrivateUser());
324 serviceContext.setPathFriendlyURLPublic(
325 PortalUtil.getPathFriendlyURLPublic());
326 serviceContext.setPathMain(PortalUtil.getPathMain());
327 serviceContext.setPlid(themeDisplay.getPlid());
328 serviceContext.setPortalURL(
329 PortalUtil.getPortalURL(portletRequest));
330 serviceContext.setSignedIn(themeDisplay.isSignedIn());
331 serviceContext.setTimeZone(themeDisplay.getTimeZone());
332
333 User user = themeDisplay.getUser();
334
335 serviceContext.setUserDisplayURL(user.getDisplayURL(themeDisplay));
336 serviceContext.setUserId(user.getUserId());
337 }
338
339 serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
340
341
342
343 Map<String, Serializable> attributes = new HashMap<>();
344
345 Enumeration<String> enu = portletRequest.getParameterNames();
346
347 while (enu.hasMoreElements()) {
348 String param = enu.nextElement();
349
350 String[] values = portletRequest.getParameterValues(param);
351
352 if (ArrayUtil.isNotEmpty(values)) {
353 if (values.length == 1) {
354 attributes.put(param, values[0]);
355 }
356 else {
357 attributes.put(param, values);
358 }
359 }
360 }
361
362 serviceContext.setAttributes(attributes);
363
364
365
366 String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
367
368 serviceContext.setCommand(cmd);
369
370
371
372 String currentURL = PortalUtil.getCurrentURL(portletRequest);
373
374 serviceContext.setCurrentURL(currentURL);
375
376
377
378 long formDateLong = ParamUtil.getLong(portletRequest, "formDate");
379
380 if (formDateLong > 0) {
381 Date formDate = new Date(formDateLong);
382
383 serviceContext.setFormDate(formDate);
384 }
385
386
387
388 ModelPermissions modelPermissions = ModelPermissionsFactory.create(
389 portletRequest);
390
391 if (!modelPermissions.isEmpty()) {
392 serviceContext.setModelPermissions(modelPermissions);
393 }
394 else {
395 boolean addGroupPermissions = ParamUtil.getBoolean(
396 portletRequest, "addGroupPermissions");
397 boolean addGuestPermissions = ParamUtil.getBoolean(
398 portletRequest, "addGuestPermissions");
399 String[] groupPermissions = PortalUtil.getGroupPermissions(
400 portletRequest);
401 String[] guestPermissions = PortalUtil.getGuestPermissions(
402 portletRequest);
403
404 serviceContext.setAddGroupPermissions(addGroupPermissions);
405 serviceContext.setAddGuestPermissions(addGuestPermissions);
406 serviceContext.setGroupPermissions(groupPermissions);
407 serviceContext.setGuestPermissions(guestPermissions);
408 }
409
410
411
412 HttpServletRequest request = PortalUtil.getHttpServletRequest(
413 portletRequest);
414
415 String portletId = PortalUtil.getPortletId(portletRequest);
416
417 PortletPreferencesIds portletPreferencesIds =
418 PortletPreferencesFactoryUtil.getPortletPreferencesIds(
419 request, portletId);
420
421 serviceContext.setPortletPreferencesIds(portletPreferencesIds);
422
423
424
425 Map<String, String> headerMap = new HashMap<>();
426
427 enu = request.getHeaderNames();
428
429 while (enu.hasMoreElements()) {
430 String header = enu.nextElement();
431
432 String value = request.getHeader(header);
433
434 headerMap.put(header, value);
435 }
436
437 serviceContext.setHeaders(headerMap);
438
439 serviceContext.setRemoteAddr(request.getRemoteAddr());
440 serviceContext.setRemoteHost(request.getRemoteHost());
441 serviceContext.setRequest(request);
442
443
444
445 Map<String, String[]> parameterMap = portletRequest.getParameterMap();
446
447 List<Long> assetCategoryIdsList = new ArrayList<>();
448
449 boolean updateAssetCategoryIds = false;
450
451 for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
452 String name = entry.getKey();
453
454 if (name.startsWith("assetCategoryIds")) {
455 updateAssetCategoryIds = true;
456
457 long[] assetVocabularyAssetCategoryIds = StringUtil.split(
458 ParamUtil.getString(portletRequest, name), 0L);
459
460 for (long assetCategoryId : assetVocabularyAssetCategoryIds) {
461 assetCategoryIdsList.add(assetCategoryId);
462 }
463 }
464 }
465
466 if (updateAssetCategoryIds) {
467 long[] assetCategoryIds = ArrayUtil.toArray(
468 assetCategoryIdsList.toArray(
469 new Long[assetCategoryIdsList.size()]));
470
471 serviceContext.setAssetCategoryIds(assetCategoryIds);
472 }
473
474 boolean assetEntryVisible = ParamUtil.getBoolean(
475 portletRequest, "assetEntryVisible", true);
476
477 serviceContext.setAssetEntryVisible(assetEntryVisible);
478
479 String assetLinkEntryIdsString = request.getParameter(
480 "assetLinksSearchContainerPrimaryKeys");
481
482 if (assetLinkEntryIdsString != null) {
483 long[] assetLinkEntryIds = StringUtil.split(
484 assetLinkEntryIdsString, 0L);
485
486 serviceContext.setAssetLinkEntryIds(assetLinkEntryIds);
487 }
488
489 Double assetPriority = ParamUtil.getDouble(request, "assetPriority");
490
491 serviceContext.setAssetPriority(assetPriority);
492
493 String assetTagNamesString = request.getParameter("assetTagNames");
494
495 if (assetTagNamesString != null) {
496 String[] assetTagNames = StringUtil.split(assetTagNamesString);
497
498 serviceContext.setAssetTagNames(assetTagNames);
499 }
500
501
502
503 int workflowAction = ParamUtil.getInteger(
504 portletRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
505
506 serviceContext.setWorkflowAction(workflowAction);
507
508 return serviceContext;
509 }
510
511 public static ServiceContext getInstance(
512 String className, HttpServletRequest request)
513 throws PortalException {
514
515 ServiceContext serviceContext = getInstance(request);
516
517
518
519 String[] groupPermissions = PortalUtil.getGroupPermissions(
520 request, className);
521 String[] guestPermissions = PortalUtil.getGuestPermissions(
522 request, className);
523
524 if (groupPermissions != null) {
525 serviceContext.setGroupPermissions(groupPermissions);
526 }
527
528 if (guestPermissions != null) {
529 serviceContext.setGuestPermissions(guestPermissions);
530 }
531
532
533
534 Map<String, Serializable> expandoBridgeAttributes =
535 PortalUtil.getExpandoBridgeAttributes(
536 ExpandoBridgeFactoryUtil.getExpandoBridge(
537 serviceContext.getCompanyId(), className),
538 request);
539
540 serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
541
542 return serviceContext;
543 }
544
545 public static ServiceContext getInstance(
546 String className, PortletRequest portletRequest)
547 throws PortalException {
548
549 ServiceContext serviceContext = getInstance(portletRequest);
550
551
552
553 String[] groupPermissions = PortalUtil.getGroupPermissions(
554 portletRequest, className);
555 String[] guestPermissions = PortalUtil.getGuestPermissions(
556 portletRequest, className);
557
558 if (groupPermissions != null) {
559 serviceContext.setGroupPermissions(groupPermissions);
560 }
561
562 if (guestPermissions != null) {
563 serviceContext.setGuestPermissions(guestPermissions);
564 }
565
566
567
568 Map<String, Serializable> expandoBridgeAttributes =
569 PortalUtil.getExpandoBridgeAttributes(
570 ExpandoBridgeFactoryUtil.getExpandoBridge(
571 serviceContext.getCompanyId(), className),
572 portletRequest);
573
574 serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
575
576 return serviceContext;
577 }
578
579 public static ServiceContext getInstance(
580 String className, UploadPortletRequest uploadPortletRequest)
581 throws PortalException {
582
583 ServiceContext serviceContext = getInstance(uploadPortletRequest);
584
585
586
587 String[] groupPermissions = PortalUtil.getGroupPermissions(
588 uploadPortletRequest, className);
589 String[] guestPermissions = PortalUtil.getGuestPermissions(
590 uploadPortletRequest, className);
591
592 if (groupPermissions != null) {
593 serviceContext.setGroupPermissions(groupPermissions);
594 }
595
596 if (guestPermissions != null) {
597 serviceContext.setGuestPermissions(guestPermissions);
598 }
599
600
601
602 Map<String, Serializable> expandoBridgeAttributes =
603 PortalUtil.getExpandoBridgeAttributes(
604 ExpandoBridgeFactoryUtil.getExpandoBridge(
605 serviceContext.getCompanyId(), className),
606 uploadPortletRequest);
607
608 serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
609
610 return serviceContext;
611 }
612
613 }