View Javadoc
1   /*
2    * Copyright (C) 2003-2010 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.social.core.processor;
18  
19  import java.util.List;
20  import java.util.Map;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  import org.exoplatform.container.xml.InitParams;
25  import org.exoplatform.portal.config.UserPortalConfigService;
26  import org.exoplatform.portal.webui.util.Util;
27  import org.exoplatform.social.core.BaseActivityProcessorPlugin;
28  import org.exoplatform.social.core.activity.model.ExoSocialActivity;
29  import org.exoplatform.social.core.identity.model.Identity;
30  import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
31  import org.exoplatform.social.core.service.LinkProvider;
32  
33  /**
34   * A processor that substitute @username expressions by a link on the user profile
35   * @author <a href="mailto:patrice.lamarque@exoplatform.com">Patrice Lamarque</a>
36   * @version $Revision$
37   */
38  public class MentionsProcessor extends BaseActivityProcessorPlugin {
39  
40    private static final Pattern pattern = Pattern.compile("@([^\\s]+)|@([^\\s]+)$");
41  
42    private UserPortalConfigService userPortalConfigService;
43  
44    public MentionsProcessor(InitParams params, UserPortalConfigService userPortalConfigService) {
45      super(params);
46      this.userPortalConfigService = userPortalConfigService;
47    }
48  
49    public void processActivity(ExoSocialActivity activity) {
50      if (activity != null) {
51        String portalOwner = null;
52        try{
53          portalOwner = Util.getPortalRequestContext().getPortalOwner();
54        } catch (Exception e){
55          //default value for testing and social
56          portalOwner = userPortalConfigService.getDefaultPortal();
57        }
58        activity.setTitle(substituteUsernames(portalOwner, activity.getTitle()));
59        activity.setBody(substituteUsernames(portalOwner, activity.getBody()));
60        Map<String, String> templateParams = activity.getTemplateParams();
61        List<String> templateParamKeys = getTemplateParamKeysToFilter(activity);
62        for(String key : templateParamKeys){
63          templateParams.put(key, substituteUsernames(portalOwner, templateParams.get(key)));
64        }
65      }
66    }
67  
68    /*
69     * Substitute @username expressions by full user profile link
70     */
71    private String substituteUsernames(String portalOwner, String message) {
72      if (message == null || message.trim().isEmpty()) {
73        return message;
74      }
75      //
76      Matcher matcher = pattern.matcher(message);
77  
78      // Replace all occurrences of pattern in input
79      StringBuffer buf = new StringBuffer();
80      while (matcher.find()) {
81        // Get the match result
82        String username = matcher.group().substring(1);
83        if (username == null || username.isEmpty()) {
84          continue;
85        }
86        Identity identity = LinkProvider.getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, username, false);
87        if (identity == null || identity.isDeleted() || !identity.isEnable()) {
88          continue;
89        }
90        try {
91          username = LinkProvider.getProfileLink(username, portalOwner);
92        } catch (Exception e) {
93          continue;
94        }
95        // Insert replacement
96        if (username != null) {
97          matcher.appendReplacement(buf, username);
98        }
99      }
100     if (buf.length() > 0) {
101       matcher.appendTail(buf);
102       return buf.toString();
103     }
104     return message;
105   }
106 }