1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.social.core.service;
18
19 import org.apache.commons.lang.StringEscapeUtils;
20 import org.apache.commons.lang.Validate;
21 import org.exoplatform.commons.utils.CommonsUtils;
22 import org.exoplatform.container.PortalContainer;
23 import org.exoplatform.portal.mop.SiteType;
24 import org.exoplatform.services.log.ExoLogger;
25 import org.exoplatform.services.log.Log;
26 import org.exoplatform.social.core.identity.model.Identity;
27 import org.exoplatform.social.core.identity.model.Profile;
28 import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
29 import org.exoplatform.social.core.identity.provider.SpaceIdentityProvider;
30 import org.exoplatform.social.core.manager.IdentityManager;
31 import org.exoplatform.social.core.model.AvatarAttachment;
32 import org.exoplatform.social.core.model.BannerAttachment;
33 import org.exoplatform.social.core.space.model.Space;
34 import org.exoplatform.social.core.space.spi.SpaceService;
35 import org.exoplatform.web.application.RequestContext;
36 import org.exoplatform.web.url.navigation.NavigationResource;
37 import org.exoplatform.web.url.navigation.NodeURL;
38
39 import java.io.UnsupportedEncodingException;
40 import java.net.URLEncoder;
41
42
43
44
45
46
47 public class LinkProvider {
48 public static final String RESOURCE_URL = "/social-resources";
49 public static final String JAVASCRIPT_RESOURCE_URL = RESOURCE_URL + "/javascript/";
50 public static final String PROFILE_DEFAULT_AVATAR_URL = "/eXoSkin/skin/images/system/UserAvtDefault.png";
51 public static final String SPACE_DEFAULT_AVATAR_URL = "/eXoSkin/skin/images/system/SpaceAvtDefault.png";
52 public static final String HAS_CONNECTION_ICON =
53 RESOURCE_URL + "/eXoSkin/skin/images/themes/default/social/skin/UIManageUsers/StatusIcon.png";
54 public static final String WAITING_CONFIRMATION_ICON =
55 RESOURCE_URL + "/eXoSkin/skin/images/themes/default/social/skin/UIManageUsers/WaittingConfirm.png";
56 public static final String SPACE_MANAGER_ICON =
57 RESOURCE_URL + "/eXoSkin/skin/images/themes/default/social/skin/UIManageSpaces/Manager.png";
58 public static final String SPACE_MEMBER_ICON =
59 RESOURCE_URL + "/eXoSkin/skin/images/themes/default/social/skin/UIManageSpaces/Member.png";
60 public static final String SPACE_WAITING_CONFIRM_ICON =
61 RESOURCE_URL + "/eXoSkin/skin/images/themes/default/social/skin/UIManageSpaces/WaitingConfirm.png";
62 public static final String STARTER_ACTIVITY_AVATAR = "/eXoSkin/skin/images/themes/default/social/skin/Activity/starterAvt.png";
63 public static final String STARTER_ACTIVITY_IMAGE = "/eXoSkin/skin/images/themes/default/social/skin/Activity/starterActImg.png";
64
65 public static final String ROUTE_DELIMITER = "/";
66
67 private static Log LOG = ExoLogger.getLogger(LinkProvider.class);
68
69 private static final String BASE_URL_SOCIAL_REST_API = "/v1/social";
70
71 public LinkProvider() {
72 }
73
74
75
76
77
78
79
80
81
82 public static String getSpaceUri(final String prettyName) {
83 SpaceService spaceService = CommonsUtils.getService(SpaceService.class);
84 Space space = spaceService.getSpaceByPrettyName(prettyName);
85 RequestContext ctx = RequestContext.getCurrentInstance();
86 if (ctx != null) {
87 NodeURL nodeURL = ctx.createURL(NodeURL.TYPE);
88 NavigationResource resource = new NavigationResource(SiteType.GROUP, space.getGroupId(), space.getUrl());
89 return nodeURL.setResource(resource).toString();
90 } else {
91 return null;
92 }
93 }
94
95
96
97
98
99
100
101
102 public static String getProfileUri(final String username) {
103 return buildProfileUri(username, null, null);
104 }
105
106
107
108
109
110
111
112
113
114 public static String getProfileUri(final String username, final String portalOwner) {
115 return buildProfileUri(username, null, portalOwner);
116 }
117
118
119
120
121
122
123
124
125 public static String getProfileLink(final String username) {
126 return getProfileLink(username, null);
127 }
128
129
130
131
132
133
134
135
136
137 public static String getProfileLink(final String username, final String portalOwner) {
138 Identity identity = getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, username, true);
139 Validate.notNull(identity, "Identity must not be null.");
140
141
142 String configured_domain_url = null;
143 try {
144 configured_domain_url = CommonsUtils.getCurrentDomain();
145 } catch (NullPointerException e) {
146 configured_domain_url = null;
147 }
148
149 return new StringBuilder("<a href=\"").append((configured_domain_url != null) ? configured_domain_url : "")
150 .append(buildProfileUri(identity.getRemoteId(), null, portalOwner)).append("\" target=\"_parent\">")
151 .append(StringEscapeUtils.escapeHtml(identity.getProfile().getFullName())).append("</a>").toString();
152 }
153
154
155
156
157
158
159
160
161
162
163
164 public static String getAbsoluteProfileUrl(final String userName, final String portalName,
165 final String portalOwner, final String host) {
166 return host + buildProfileUri(userName, portalName, portalOwner);
167 }
168
169
170
171
172
173
174
175
176 public static String getUserActivityUri(final String remoteId) {
177 return getActivityUri(OrganizationIdentityProvider.NAME,remoteId);
178 }
179
180
181
182
183
184
185
186
187 public static String getUserConnectionsUri(final String remoteId) {
188 return getBaseUri(null, null) + "/connections/all-people" + ROUTE_DELIMITER + remoteId;
189 }
190
191
192
193
194
195
196
197 public static String getUserConnectionsYoursUri(final String remoteId) {
198 return getBaseUri(null, null) + "/connections/network" + ROUTE_DELIMITER + remoteId;
199 }
200
201
202
203
204
205
206
207
208 public static String getUserProfileUri(final String remoteId) {
209 return getBaseUri(null, null) + "/profile" + ROUTE_DELIMITER + remoteId;
210 }
211
212
213
214
215
216
217
218
219
220 public static String getActivityUri(final String providerId, final String remoteId) {
221 final String prefix = getBaseUri(null, null) + "/";
222 if (providerId.equals(OrganizationIdentityProvider.NAME)) {
223 return String.format("%sactivities/%s",prefix,remoteId);
224 } else if (providerId.equals(SpaceIdentityProvider.NAME)) {
225 return String.format("/%s/g/:spaces:%s/%s",getPortalName(null),remoteId,remoteId);
226 } else {
227 LOG.warn("Failed to getActivityLink with providerId: " + providerId);
228 }
229 return null;
230 }
231
232
233
234
235
236 public static String getSingleActivityUrl(String activityId) {
237 return getBaseUri(null, null) + "/activity?id=" + activityId;
238 }
239
240
241
242
243
244
245
246
247
248
249 public static String getActivityUriForSpace(final String remoteId, final String groupId) {
250 return String.format("/%s/g/:spaces:%s/%s", getPortalName(null), groupId, remoteId);
251 }
252
253
254
255
256
257
258
259
260
261 public static String buildAvatarImageUri(final Space space) {
262 return buildAvatarImageUri(space.getAvatarAttachment());
263 }
264
265
266
267
268
269
270
271
272
273 public static String buildAvatarImageUri(final String identityName) {
274 return String.format(
275 "/rest/jcr/repository/social/production/soc:providers/soc:space/soc:%s/soc:profile/soc:avatar",
276 identityName);
277 }
278
279
280
281
282
283
284
285
286 public static String escapeJCRSpecialCharacters(String string) {
287 if (string == null) {
288 return null;
289 }
290 return string.replace("[", "%5B").replace("]", "%5D").replace(":", "%3A");
291 }
292
293
294
295
296
297
298
299
300
301
302 public static String getAvatarImageSource(final PortalContainer portalContainer, final Profile profile) {
303 final AvatarAttachment avatarAttachment = (AvatarAttachment) profile.getProperty(Profile.AVATAR);
304 if (avatarAttachment != null) {
305 return buildAvatarImageUri(avatarAttachment);
306 }
307 return null;
308 }
309
310
311
312
313
314
315
316
317
318 public static String getAvatarImageSource(final Profile profile) {
319 String avatarUrl = profile.getAvatarUrl();
320 if (avatarUrl != null) {
321 return avatarUrl;
322 }
323
324 final AvatarAttachment avatarAttachment = (AvatarAttachment) profile.getProperty(Profile.AVATAR);
325 if (avatarAttachment != null) {
326 avatarUrl = buildAvatarImageUri(avatarAttachment);
327 profile.setAvatarUrl(avatarUrl);
328 getIdentityManager().saveProfile(profile);
329 return avatarUrl;
330 }
331 return null;
332 }
333
334
335
336
337
338
339
340 public static String buildAvatarImageUri(final AvatarAttachment avatarAttachment) {
341 String avatarUrl = null;
342 try {
343 if (avatarAttachment != null) {
344 final String repository = CommonsUtils.getRepository().getConfiguration().getName();
345
346 avatarUrl = escapeJCRSpecialCharacters(new StringBuilder("/").append(CommonsUtils.getRestContextName())
347 .append("/jcr/")
348 .append(repository)
349 .append("/")
350 .append(avatarAttachment.getWorkspace())
351 .append(avatarAttachment.getDataPath())
352 .append("/?upd=")
353 .append(avatarAttachment.getLastModified())
354 .toString());
355 }
356 } catch (Exception e) {
357 LOG.warn("Failed to build avatar url from avatar attachment for: " + e.getMessage());
358 }
359 return avatarUrl;
360 }
361
362
363
364
365
366
367
368
369
370
371 private static String buildProfileUri(final String userName, final String portalName, String portalOwner) {
372 return getBaseUri(portalName, portalOwner) + "/profile" + ROUTE_DELIMITER + userName;
373 }
374
375
376
377
378
379
380
381
382
383 private static String getBaseUri(final String portalName, String portalOwner) {
384 return "/" + getPortalName(portalName) + "/" + getPortalOwner(portalOwner);
385 }
386
387 private static String getSpaceBaseUri(final String portalName, String portalOwner) {
388 return "/" + getPortalName(portalName);
389 }
390
391
392
393
394
395
396
397 public static String getUserNotificationSettingUri(final String remoteId) {
398 return getBaseUri(null, null) + "/notifications" + ROUTE_DELIMITER + remoteId;
399 }
400
401
402
403
404
405
406 public static String getRedirectUri(String type) {
407 if (type.isEmpty()) {
408 return getBaseUri(null, null);
409 }
410 return getBaseUri(null, null) + "/" + type;
411 }
412
413 public static String getRedirectSpaceUri(String type) {
414 if (type.isEmpty()) {
415 return getSpaceBaseUri(null, null);
416 }
417 return getSpaceBaseUri(null, null) + "/" + type;
418 }
419
420
421
422
423
424
425 public static IdentityManager getIdentityManager() {
426 return CommonsUtils.getService(IdentityManager.class);
427 }
428
429
430
431
432
433
434
435 public static String buildAvatarURL(String providerId, String remoteId) {
436 return buildAttachmentUrl(providerId, remoteId, AvatarAttachment.TYPE);
437 }
438
439
440
441
442
443
444
445 public static String buildBannerURL(String providerId, String remoteId) {
446 return buildAttachmentUrl(providerId, remoteId, BannerAttachment.TYPE);
447 }
448
449 private static String buildAttachmentUrl(String providerId, String remoteId, String type) {
450 if (providerId == null || remoteId == null) {
451 return null;
452 }
453
454 String username = remoteId;
455
456 try {
457 username = URLEncoder.encode(username, "UTF-8");
458 } catch (UnsupportedEncodingException ex) {
459 LOG.warn("Failure to encode username for build URL", ex);
460 }
461
462 if(providerId.equals(OrganizationIdentityProvider.NAME)) {
463 return new StringBuilder("/").append(CommonsUtils.getRestContextName()).append(BASE_URL_SOCIAL_REST_API).append("/users")
464 .append("/").append(username)
465 .append("/").append(type)
466 .toString();
467 } else if(providerId.equals(SpaceIdentityProvider.NAME)) {
468 return new StringBuilder("/").append(CommonsUtils.getRestContextName()).append(BASE_URL_SOCIAL_REST_API).append("/spaces")
469 .append("/").append(username)
470 .append("/").append(type)
471 .toString();
472 }
473 return null;
474 }
475
476
477
478
479
480
481
482 private static String getPortalOwner(String portalOwner) {
483 if (portalOwner == null || portalOwner.trim().length() == 0) {
484 portalOwner = CommonsUtils.getCurrentPortalOwner();
485 }
486 return portalOwner;
487 }
488
489
490
491
492
493
494
495 private static String getPortalName(String portalName) {
496 if (portalName == null || portalName.trim().length() == 0) {
497 return PortalContainer.getCurrentPortalContainerName();
498 }
499 return portalName;
500 }
501 }